Title: Matlab Training Session 4: Control, Flow and Functions
1Matlab Training Session 4Control, Flow and
Functions
2- Course Outline
- Weeks
- Introduction to Matlab and its Interface (Sept.
27 2005) - Fundamentals (Operators)
- Fundamentals (Flow)
- Importing Data
- Functions and M-Files
- Plotting (2D and 3D)
- Statistical Tools in Matlab
- Analysis and Data Structures
- Classes on November 1st, 8th and 15th are
cancelled do to the Society for Neuroscience
(SFN) 35th annual meeting. - Course Website
- http//www.queensu.ca/neurosci/Matlab Training
Sessions.htm
3- Week 4 Lecture Outline
- Fundamentals of Matlab 4
- A. Week 3 Review
- B. Functions in Matlab
- C. Mini Project
4Matlab Scripts
- Entering Multiple Lines Without Running Them
- Matlab can execute sequences of commands stored
in files - Files that contain matlab commands have the
extension .m - .m files are written and saved in the built in
matlab m-file editor - M-files are executed from the M-file editor or
the command prompt - M-files can call other M-files
- The location path of the M-file must be set in
matlab
5Matlab Scripts
- Advantages of M-files
- Easy editing and saving of work
- Undo changes
- Readability/Portability - non executable comments
can be added using the symbol to make make
commands easier to understand - Saving M-files is far more memory efficient than
saving a workspace
6Week 2 Review
- Relational Operators
- Relational operators are used to compare two
scaler values or matrices of equal dimensions - Relational Operators
- lt less than
- lt less than or equal to
- gt Greater than
- gt Greater than or equal to
- equal
- not equal
7Condition Statements
- It is often necessary to only perform matlab
operations when certain conditions are met - Relational and Logical operators are used to
define specific conditions - Simple flow control in matlab is performed with
the If, Else, Elseif and Switch statements
8Condition Statements
- If, Else, and Elseif
- An if statement evaluates a logical expression
and evaluates a group of commands when the
logical expression is true - The list of conditional commands are terminated
by the end statement - If the logical expression is false, all the
conditional commands are skipped - Execution of the script resumes after the end
statement - Basic form
- if logical_expression
- commands
- end
9Condition Statements
- If, Else, and Elseif
- The elseif statement forces execution of the
commands below the else statement if the original
logical expression evaluates to false - Only one list of commands can be executed
- Basic form
- if logical_expression
- commands 1
- elseif logical_expression_2
- commands 2
- elseif logical_expression_3
- commands 3
- end
10Loops
- Loops are an important component of flow control
that enables matlab to repeat multiple statements
in specific and controllable ways - Simple repetition in matlab is controlled by two
types of loops - For loops
- While loops
11Loops
- For Loops
- The for loop executes a statement or group of
statements a predetermined number of times - Basic Form
- for index startincrementend
- statements
- end
- If increment is not specified, an increment
of 1 is assumed by matlab
12Loops
- While Loops
- The while loop executes a statement or group of
statements repeatedly as long as the controlling
expression is true - Basic Form
- while expression
- statements
- end
13Functions
- Building blocks of programming
- Allow code to be generic and reusable
- Design from top down but build from bottom up
- Take a set of inputs, perform a series of
manipulations and return an output
14Functions in Matlab
- In Matlab, each function is a .m file
- It is good protocol to name your .m file the same
as your function name, i.e. funcname.m - function outargsfuncname(inargs)
Function
input
output
15Simple Example
- Find the cube of a number -gt (x3)
- Type the code below into an .m file and save it
as cube.m - Set the Matlab directory appropriately
- In Matlab window, type cube(3), is the result
correct? - Now you have a reusable function that can
calculate the cube of any number
gtgt cube(3) Ans 125 gtgt cube 1.75 Ans 5.3594
function y cube(x) y xxx
16Add Some Help
- Find the cube of a number -gt (x3)
- Add commented text between the funciton
declaration and the first line of code - Now type help cube in Matlab window
gtgt help cube Put some text here
function y cube(x) Put some text here y
xxx
17Find the cube of two numbers
- Any numbers of inputs and outputs can be handled
in a function - For a simple example, extend the cube function to
accept two inputs and find the cube of each
function y1, y2 cube(x1, x2) Put some text
here y1 x1x1x1 y2 x2x2x2
gtgt cube(2,3) Ans 4 ??? gtgt a b
cube(2,3) a 8 b 27
18nargin
- Matlab will accept a function call with any
number of inputs and outputs - nargin can be used to find out how many inputs
the user has provided - It is automatically passed with the function
function y1, y2 cube(x1, x2) If nargin
1 y1 x1x1x1 y2 Nan elseif nargin
2 y1 x1x1x1 y2 x2x2x2 end
gtgt cube(2,3) Ans 4 gtgt a b cube(2,3) a 8 b
27
19return
- return terminates computation of the function and
returns whatever is calculated thus far
function y1, y2 cube(x1, x2) If nargin
1 y1 x1x1x1 y2 Nan return end y1
x1x1x1 y2 x2x2x2
gtgt cube(2,3) Ans 4 gtgt a b cube(2,3) a 8 b
27
20Find the Cube of Any Number of Numbers
- Any ideas on how to accomplish this?
- Lets further extend the function to cube a vector
of numbers - Before we thought of the variables as scalars,
now rethink the power of vectors
gtgt cube(2) Ans 8 gtgt cube(2 3) Ans 8 27
function y cube(x) Put some text here y
x.3
21Mini-Project
- Raising any number of numbers to the nth power
- Inputs
- A vector of numbers to be raised (N1Nm)
- A vector of powers (P1Pm)
- Outputs
- A vector of raised values (N1P1 NmPm)
- An error flag 1 if error in calculation, 0 if
successful - Caveats
- If only one input is provided, the function
should square each entry, so output (N12Nm2)
and error flag is 0 - If the length of N and P are not the same, this
is an error, return anything in the output vector
and a 1 in the error flag - Make sure to comment and document the function
22Mini-Project
- Lets combine what we have learned thus far in the
course - Loops, If Statements, Functions
- Basic Matlab Operators
- Should finish this in class today
- If you get through this then you are doing well
23Mini-Project
- Plan before you code
- Remember that xnxxxx x (n times)
- Ask questions and take your time
24Solution
function y, e raise(x,n) y
ones(1,length(x)) if nargin 1 y e
raise(x,2ones(1,length(x))) return elseif
nargin 2 if(length(x)length(n))
y NaN e 1 return end
for(i1length(x)) for(j1n(i))
y(i) y(i)x(i) end end e
0 return end
25Getting Help
- Help and Documentation
- Digital
- Accessible Help from the Matlab Start Menu
- Updated online help from the Matlab Mathworks
website - http//www.mathworks.com/access/helpdesk/help/tech
doc/matlab.html - Matlab command prompt function lookup
- Built in Demos
- Websites
- Hard Copy
- Books, Guides, Reference
- The Student Edition of Matlab pub. Mathworks Inc.