Loops :
- 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
For Loops
- The for loop executes a statement or group of statements a predetermined number of times
Basic form:
for index = start:increment:end
statements
end
** If ‘increment’ is not specified, an increment of 1 is assumed by matlab
Example
1 2 3 |
for i = 1:1:100 x(i) = 0 End |
** Assigns 0 to the first 100 elements of vector x
** If x does not exist or has fewer than 100 elements, additional space will be automatically allocated
- Loops can be nested in other loops
1 2 3 4 5 6 7 |
A = [ ] for i = 1:m for j = 1:n A(i,j) = i + j end End |
**Creates an m by n matrix A whose elements are the sum of their matrix position.
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
Example
1 2 3 4 5 6 7 8 |
A = 6 B = 5 while A > 0 & B < 10 A = A – 1 B = B + 2 end |
Iteratively decrements A and increase B until the two conditions of the while loop are TRUE
** Be very careful to ensure that your while loop will eventually reach its termination condition to prevent an infinite loop
Breaking out of loops
- The ‘break’ command instantly terminates a for and while loop
- When a break is encountered by matlab, execution of the script continues outside and after the loop
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 |
A = 6 B = 15 count = 1 while A > 0 & B < 1000 A = A + 1 B = B + 2 count = count + 1 if count > 100 break end end |
Break out of the loop after 100 repetitions if the while condition has not been met
Base Workspace:
- The base workspace stores variables created at the command line
- Base workspace variables exist until ‘clear’ command executed or during end of MATLAB® session
Function Workspace:
- Functions create their own function workspace
- Local variables are known only within that function workspace
- Local variables are not available at the command line or to any other function
Global Workspace:
- Global variables are stored separately from the base and function workspaces
MAT-Files
- These are binary MATLAB format files that store workspace variables.
- To see the variables in a MAT-file before loading the file into your workspace, click the file name in the Current Folder browser. Information about the variables appears in the Details Panel.
Save, Load, and Delete Workspace Variables: MAT-File
- The workspace is not maintained across sessions of MATLAB
- Save any or all of the variables in the current workspace to a MAT-file (.mat).
- Load MAT-files at a later time during the current MATLAB session, or during another session, if you want to reuse the workspace variables.
Passing Arguments: Link
- Uses function input and output arguments
Nested Functions:
- A nested function has access to the workspaces of all functions in which it is nested
Persistent Variables:
- Declare a variable within a function as ‘persistent’ to retains its value
- Persistent variables are equivalent to static variables in other programming languages
- Below operations clear the persistent variables for a function:
- clear all, clear functionname
- To prevent clearing persistent variables, lock the function file using ‘mlock’
Global Variables:
- Global variables are stored separately from the base and function workspaces
- Global variables are accessible from any functions or from the command line
- Declare variables with ‘global’ keyword before using them