Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4...

147
Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易易易易” ).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as the BASE of the Teaching Lecture Note.

Transcript of Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4...

Page 1: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Basic MATLAB Programming Course

(R0.1– Marked/Revised for “Matlab- 易學解析” ).ppt 2010-Q4 )

NOTE: This R0.1 version is incomplete now. It is just used as the BASE of the Teaching Lecture Note.

Page 2: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

What we will learn in this session

(Week 01 -02 ) The basic MATLAB interface. Basic commands. Declaring & manipulating variables. Plotting graphs. Conditional Operators. (week03/04) Functions. (week 05– )

Page 3: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Basic MATLAB Interface

1) Tool (GUI) –How to Use

2) Simple Demo/Lab.

Page 4: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.
Page 5: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Command window: Type your instructions here and press ENTER to execute them.

Page 6: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example: Declare a column matrix withvalues 1,2 and 3.

Page 7: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Command history: a list of instructions executed by MATLAB is shown here.

Page 8: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Workspace: shows a list of variables created by MATLAB.As you can see, the value of ‘aaa’is shown.

Page 9: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Another way to create a variable Is to press this button.

Page 10: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

MATLAB will prompt you to enter the variable name.

Page 11: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

As you can see, the variable name has been changed to bbb.

Page 12: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

To assign a value to bbb, you can do it intwo ways: 1) Using the command window.

2) Or by double clicking on bbb.

Page 13: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

When you click on bbb, the variableeditor window appears. You can typein new values into bbb by filling in the cells.

Page 14: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

An example is shown here.Try and do it yourself.

Page 15: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

To display variables at the console, you can type the variable name, or you can type disp(variable_name).

Page 16: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

To clear all variables from memory and close all figures, use the clear, close all command.

Page 17: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

As you can see, all workspace variables are deleted when you execute this command.

Page 18: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

To clear the command window,use the clc (clear console) command.

Page 19: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

As you can see, all console entries are deleted when you execute this command.

Page 20: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

If you want to see help,you can type help at the command window.

Page 21: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Or you can press F1to display the help window. Click on Open Help Browser to search for a specific function.

Page 22: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example: search for function mean

Page 23: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

To create an m-file, 1) type edit at the command window, or 2) Press this button.

Page 24: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

The previous command willdisplay the editor window.The editor creates an m-filethat can be used to write your MATLAB programs.

Page 25: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

To execute a program, pressthe RUN button.

Page 26: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

This window will appear. Press the Change Directory button.

Page 27: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

You can see that the program hascreated two new variables in the Workspace.

Page 28: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Basic Commands

Page 29: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Variables

MATLAB can be used to initialize and manipulate many types of variables.Single value. (scalar -- 純量 )Matrix ( 向量 ) vs. ARRAY ( 陣列 )String ( 字串 )

Page 30: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Declaring Single Variables

To declare single variables, type in a variable name and type in its value.

MATLAB will decide on the data type automatically, so you don’t have to declare its data type.

Example:var1 = 3; thisIsAVariable = 56;

Page 31: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.
Page 32: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Declaring Single Variables

Variables cannot have numbers or symbols in front of them.

Example of illegal variable names:1var#aaa

Page 33: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.
Page 34: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Matrix Variables

Matrix variables are initialized similar to single variables.

The values in a matrix variable is defined in square brackets.

Example:aaa = [1,2,3,4];bbb = [1;2;3;4];

Page 35: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Row Matrix

To create a row matrix, use the comma (,) to separate the values.

Example:rowMatrix = [1,2,3,4,5];

Page 36: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example

Page 37: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Try It Yourself

Create a row matrix named var1 with the values of 1, 3, 5 in it.

Create a row matrix named mat1 with the values of 10, 20, 30, 40, 50, 60 in it.

Create a row matrix named var2 with the values of 1, 3, 5, 6, 8, 10, 11 in it.

Page 38: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Column Matrix

To create a column matrix, use the semicolon to separate the values.

Example:colMatrix = [1;2;3;4;5];

Page 39: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example

Page 40: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Try It Yourself

Clear and close all variables, and clear console. Create a column matrix named col1 with the

values of 2, 6, 9 in it. Create a column matrix named mat3 with the

values of 15, 23, 37, 48, 59, 61 in it. Create a column matrix named colMatrix with

the values of 1, 3, 5, 6, 8, 10, 11 in it.

Page 41: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Regular Matrix

To create a regular matrix, use the comma to separate each value in a row, and a semicolon to enter the value for a new row.

Example:mat1 = [1,2,3;4,5,6;7,8,9];

Page 42: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example

Page 43: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Try It Yourself

Create this matrix:

43

21A

Page 44: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Try It Yourself

Create this matrix:

535

542

542

matrixB

Page 45: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Accessing Matrix Values

To access a specific value inside a matrix, use this command:

matrixName(rowNumber, colNumber) Example: to access a value inside row 3

and column 2.matrixName(3,2) ( 橫列號 , 直行號 )

Page 46: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.
Page 47: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Try It Yourself

Create this matrix:

Try to get values 9, 3 and 1 from the matrix and save it into three variables.

176

1098

543

matrixB

Page 48: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Accessing Whole Columns and Rows To get a whole column, use this command: varA = matName(:,colNumber); To get a whole row, use this command: varA = matName(rowNumber,:); Row == 橫列 , Column == 直行

Page 49: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example

Page 50: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Try it Yourself

Create this matrix:

Get all the values from row 3 and save it into a new variable.

Get all the values from column 1 and save it into a new variable.

176

1098

543

matrixB

Page 51: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Creating a Matrix of Zeros

To create a matrix of zeros, use the zeros command.

Example: create a 6 X 5 matrix of zeros. zeros(6,5)

Page 52: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example

Page 53: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Creating a Matrix of Ones

To create a matrix of ones, use the ones command.

Example: create a 5 X 3 matrix of ones. ones(5,3)

Page 54: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example

Page 55: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Creating a Matrix of Random Numbers To create a matrix of random numbers,

use the rand command. Example: create a 4 X 4 matrix of random

numbers. rand(4,4) vs. rand ( 4) vs. rand(4,3)

-- See the differences

Page 56: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example

Page 57: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Getting the Size of the Matrix

To get the size of the matrix, use the size command.

Example: to get the size of matrix aaa. [numRow, numCol] = size(aaa);

use colon ( : ) to get [1 : 6] array. aVector = [ 1: 6] aArray = [1:6, 10:20]Use size (varName) to see the differences

Page 58: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example

Page 59: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Transposing a Matrix

A transpose operation changes the column of a matrix into rows, and rows into columns.– 轉置

To do a transpose, use the single quote operator ( ‘) . newA = AA’ newAT = newA’

Page 60: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example: Transposing a Row Matrix

Page 61: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example: Transposing a Column Matrix

Page 62: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example: Transposing a Regular Matrix

Page 63: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Finding the Maximum Value

To find the maximum value for a matrix, use the max function.

Example: find the maximum value in matrix aaa.

maxVal = max(aaa);

Page 64: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example

Max finds the maximum value in each column

When it is run again on the result, it returns the single-largest value in the matrix.

Page 65: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Finding the Minimum Value

To find the minimum value for a matrix, use the min function.

Example: find the minimum value in matrix aaa.

minVal = min(aaa);

Page 66: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example

Min finds the minimum value in each column

When it is run again on the result, it returns the minimum value in the matrix.

Page 67: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Finding the Sum of Columns

To find the sum of each column, use the sum command.

Example: find the sum for each column in matrix aaa.

colSum = sum(aaa);

Page 68: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example

Page 69: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Adding Matrices

To add matrices, use the + operator. Example: add matrices A and B.

A + B. Make sure that the matrices are the same

size. Note: size means( m row x n col.) ,

Ex. display : 4 3 =>> (4 x 3)

Page 70: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example

Page 71: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Subtracting Matrices

To subtract matrices, use the - operator. Example: subtract matrix B from A.

A - B. Make sure that the matrices are the same

size.

Page 72: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example

Page 73: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Multiplying Matrices

To multiply matrices, use the .* operator. Example: multiply matrices A and B.

A .* B

Make sure that the matrices are the same size.

DEMO: One-to-One (element-by-element)

Page 74: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example

Page 75: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Dividing Matrices

To divide matrices, use the ./ operator. Example: divide matrices A with B.

A ./ B. Make sure that the matrices are the same

size. (DEMO )

Page 76: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example

Page 77: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Sorting Matrices

To sort a matrix, use the sort command. Example: sort matrix A in ascending order.

B = sort(A,’ascend | descend’)Default is ascending mode.( 遞增 )Ex: BD = sort (AA, ‘descend’)

BA = sort (AA ) (DEMO) – help sort vs. doc sort

Page 78: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example: Sorting a Row Matrix

Page 79: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example: Sorting a Column Matrix

Page 80: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example: Sorting a Regular Matrix

Page 81: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example: Sorting a Row Matrix in Descend Mode

Page 82: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Flipping a Matrix ( 暫跳 _Skip Out)

A matrix can be flipped using the flipud or fliplr commands.

Command flipud flips the matrix in UP/DOWN direction.

Command fliplr flips the matrix in LEFT/RIGHT direction.

Page 83: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example: flipud

Page 84: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example: fliplr

Page 85: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Strings (* - 中文例 )

MATLAB also can accept and manipulate string variables.

A string is defined by enclosing it in single quotes (not Double quote ? ).

Example: aString = ‘Hello World!’ (DEMO) – whos vs. who aSring

Page 86: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example: Initializing a String

Page 87: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Converting a String to Lowercase

To convert a string to lowercase, use the lower command.

Example: change string in matrix A to lowercase:B = lower(A)

Page 88: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example: Change String to Lowercase

Page 89: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Converting a String to Uppercase

To convert a string to uppercase, use the upper command.

Example: change string in matrix A to uppercase:B = upper(A)

Page 90: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example: Change String to Uppercase

Page 91: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Concatenate Strings

Concatenating string means merging two or more strings together.

To concatenate strings, use the strcat command.

Example: to concatenate str1 and str2:newStr = strcat(str1,str2)

Page 92: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example: Concatenate String

Page 93: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Replace String

To replace part of the string with a new value, use the strrep command.

Example: replace the word ‘lama’ with the word ‘baru’ in the string str1.strrep(str1,’lama’,’baru’)

Page 94: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example: Replace String

Page 95: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Plot

Week02/03 –Primary work/Homework/Examples

(DEMO for Matlab features)

Page 96: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Plot Function

The plot function can be used to draw the relationship between two variables.

Format:plot(x,y,lineParameters)

Page 97: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example: Plot the values of a random matrixclear, close allclc // clear command windows xxx = 1:100 yyy = rand(1,100) // change 100 to smaller number plot(xxx,yyy) // see what happens ?

Page 98: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example

Page 99: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example: Draw sin(x)

clear, close allclc x = 0:pi/36:10*piy = sin(x) plot(x,y,‘m')

Page 100: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example

Page 101: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Plot Styles

Page 102: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Plot Styles

Page 103: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example: Plotting the lines using line parametersclear, close allclc xxx = 1:100 yyy = rand(1,100) plot(xxx,yyy)figure, plot(xxx,yyy,'g:') % the command figure is figure, plot(xxx,yyy,'r--')% used to create a newfigure, plot(xxx,yyy,':mo')% figure each time a plot

% is made.

Page 104: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Try It Yourself

clear, close allclc xxx = 1:100 yyy = rand(1,100) plot(xxx,yyy)figure, plot(xxx,yyy,'g:') % the command figure is figure, plot(xxx,yyy,'r--')% used to create a newfigure, plot(xxx,yyy,':mo')% figure each time a plot

% is made.

Page 105: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Drawing two plots in the same figureclear, close allclc xxx = 1:100yyy = rand(1,100) aaa = 1:100bbb = rand(1,100) plot(xxx,yyy,'r')hold onplot(aaa,bbb,'-.gv')hold off

Page 106: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example: Drawing Bar Graphs

clear, close all

clc

x = 0:pi/36:2*pi

y = cos(x)

bar(x,y,'b')

Page 107: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example

Page 108: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example: Drawing a Stair-Step Plot

clear, close all

clc

x = -10:0.5:10

y = x.^2 + 2.*x + 2

stairs(x,y,'b')

Page 109: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example

Page 110: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Image Manipulation

(Skip out – as Week10+)

Now, Next section:-

Branch & Loop- IF..ELSE / WHILE

Page 111: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Reading and Displaying an Image

To read an image, use the imread command.

Example: read an image file named ‘c:\aaa.jpg’ and store it in matrix A.A = imread(‘c:\aaa.jpg’)

To display an image, use the imshow(A) command.

Page 112: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example: Read and Display Image

Page 113: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Manipulating an Image: Convert to Grayscale To convert an RGB image to grayscale,

use the rgb2gray command. Example: convert image A to grayscale:

B = rgb2gray(A)

Page 114: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example: Convert to Grayscale

clear, close allclc aaa = imread('d:\My Documents\My Pictures\50294.jpg');

figure, imshow(aaa)bbb = rgb2gray(aaa)figure, imshow(bbb)

Page 115: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.
Page 116: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Saving an Image

To save an image, use the imwrite command.

Example: write image a into file xxx.jpg: imwrite(A,’xxx.jpg’,’jpg’)

Page 117: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Conditional Operators

Week 03/04 lecture

Page 118: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

If…else Operator

The if…else operator tests a condition. If the condition is true, then execute the if

block. If the condition is false, execute the else

block.

Page 119: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

If…else Operator

if (condition) % if blockelse % else blockend

% conditions that can be tested% == : is equal to% ~= : is not equal to% > : larger than% >= : larger than or equal% <= : less than or equal% < : less than

Page 120: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example (*-- Enhanced Examples Needed)

clear, close allclc aaa = rand(1,100);bbb = 1:1:100 color = 1; if (color == 1) % if block figure, plot(bbb,aaa,':r');else % else block figure, plot(bbb,aaa,'b');end

Page 121: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example

clear, close allclc x = 3; if (x > 5) disp('The number is more than 5.')elseif (x == 5) disp('The number is equal to 5.')else disp('The number is less than 5.')end

Page 122: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

For loop

Used to repeat a set of statements multiple times.

The for loop format is: for(startingvalue:increment:endingvalue)

Page 123: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

For Loop (*-- Enhanced Examples Needed)

clear, close allclc % i is the value of the counterfor i = initial_value:increment:ending_value % statements in this block will be executed until i % reaches the ending_value

end

Page 124: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example: Display value inside for loopclear, close allclc for i = 1:1:15 st1 = strcat('The value of i inside the loop is: ',int2str(i));

disp(st1)end

Page 125: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example: Display “Hello World” 10 Times

st1 = 'Hello World!';

for i = 1:1:10

disp(st1)

end

Page 126: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Ex: Check Value Inside Matrixclear, close allclc matA = [1.4,4.2,6.7,7.0; 5.5,6.7,8.9,3.0; 0.6,6.12,5.44,8.94][row,col] = size(matA) for i = 1:1:row for j = 1:1:col currNo = matA(i,j); st1 = strcat('The value being tested is: ',num2str(currNo),'.'); disp(st1) if (currNo > 3) disp('The current value is larger than 3.') else disp('The current value is less or equal than 3.') end endend

Page 127: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

While loop (* Revised needed)

Used to repeat a set of statements while the tested condition is true.

The while loop format is:while(condition)

The tested condition is the same as if…else

Page 128: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Conditions that can be tested

% conditions that can be tested% == : is equal to% ~= : is not equal to% > : larger than% >= : larger than or equal% <= : less than or equal% < : less than

Page 129: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example: Display value inside for loopclear, close allclc counter = 1; while(counter <= 15) st1 = strcat('The value of i inside the loop is:

',int2str(counter)); disp(st1) counter = counter + 1;end

Page 130: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example: Display “Hello World” 10 Times clear, close allclc st1 = 'Hello World!';counter = 1; while(counter <= 10) disp(st1) counter = counter + 1;end

Page 131: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Functions

Page 132: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Functions (* too simple !!/ Need to REVISE)

A complex program may be divided into several functions.

These functions can improve readability of the code, as well as promote re-usability of the code.

Each function must be saved into a different file, with the filename similar to the function.

Page 133: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Function

The format of a function is: function returnValue = fcnName(inputValue)

Page 134: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example

function hasil = addFcn(number1, number2)

hasil = number1 + number2;

end

% to call this function, call the fcn name

% using your own parameters.

% e.g. c = addFcn(3,4)

% e.g. c = addFcn(a,b)

Page 135: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Example: Using Functions clear, close allclc number1 = 4;number2 = 5;selection = 1; if selection == 1 hasil = addFcn(number1,number2);elseif selection == 2 hasil = subFcn(number1,number2);elseif selection == 3 hasil = mulFcn(number1,number2);elseif selection == 4 hasil = divFcn(number1,number2);else disp('The selection is invalid.')end disp(strcat('The result is:', num2str(hasil)));

Page 136: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

addFcn

function hasil = addFcn(number1, number2)

hasil = number1 + number2;

end

Page 137: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

subFcn

function hasil = subFcn(number1, number2)

hasil = number1 - number2;

Page 138: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

mulFcn

function hasil = mulFcn(number1, number2)

hasil = number1 * number2;

Page 139: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

divFcn

function hasil = divFcn(number1, number2)

hasil = number1 / number2;

Page 140: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Sample Problems

Note:

Just 5+ Easy samples here.

More will be added.

Page 141: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Problem 1

Write a program that calculates and displays the volume of a sphere when given the radius. The volume calculation must be performed in a function called calcSphereVolume.

The formula for volume is:

3

3

4jV

Page 142: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Problem 2

Write a program that plots the function:

for values of x = -50 to 50

6323 xxxy

Page 143: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Problem 3

Generate a 100x50 matrix of random numbers called A.

For each value inside the matrix, if the value is above 0.5, change its value to 1. If it is below 0.5, change its value to zero.

Then, count and display how many ones inside the matrix.

Page 144: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Problem 4

Use the plot function to draw this figure:

Page 145: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

Problem 5

Write a program that reads an image and converts the image into its negative.

To convert to a negative image, for each pixel value: If the current pixel value is black (0), then change it to

white (255). If the current pixel value is white (255), then change it

to black (0). Show the original image and the changed

image.

Page 146: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

White = 255, Black = 0

Page 147: Basic MATLAB Programming Course (R0.1– Marked/Revised for “Matlab- 易學解析 ”).ppt 2010-Q4 ) NOTE: This R0.1 version is incomplete now. It is just used as.

The End

(R01—incomplete -2010Q4)