Matlab Tutorial Session 1, Date: January 5th and 6th 2015
Learning Objectives:
1. Learn about the Matlab GUI and its components – Command prompt, workspace and
variable space
2. How numbers and strings are stored in Matlab memory
3. Entering vectors and matrices in Matlab
4. Loop and Conditional constructs
The Matlab GUI:
Command Prompt: where commands to be immediately executed are entered
Workspace: where variables stored in Matlab memory can be visually verified
Variable space: A component of the workspace where values held by variables are displayed
when corresponding variables in the workspace are selected by double clicking
Working with Matlab:
Set the current folder using the path toolbar beneath the toolstrip
To enter commands to be executed in Matlab:
1. Open editor by selecting New script, New or Open from the Home toolstrip (or)
2. Directly enter commands in the command prompt and press enter to execute
Memory storage in Matlab:
Numbers and strings are stored as 1-D vectors and/or multidimensional matrices called arrays
Entering Matrices in Matlab:
Matrices are entered within square brackets
Rows are delimited by a semicolon
Columns are separated by a space or comma
Note: Except inside matrices (i.e. square brackets), Matlab is insensitive to spaces i.e. Matlab
ignores spaces everywhere else
Note: Matlab distinguishes between upper and lower case variables i.e. A is different from a
Conditional Statements:
if condition 1
clause 1
else
clause 2
end
Note: All constructs end with an end keyword
Note: else clause may be omitted if not required
Note: All keywords begin with lower case alphabets and are followed by a space
Testing more than 1 condition:
if condition 1
clause 1;
elseif condition 2
clause 2;
else
clause 3;
end
Loop Constructs:
Loops are used to iterate over a list or an array: for and while
The for loop:
for iterator = start value : end value
body of for loop;
end
Note: iterator can be any variable name but a keyword
Important: All statements in the body or the clauses should end with a semicolon to suppress
output
References:
Matlab help documentation
Module 1, pages 539-557, Process control, modeling, design and simulation by Wayne Bequette
Matlab Tutorial Session 2, Date: January 7th and 8th 2015
Learning Objectives:
1. Functional programming in Matlab
2. Plotting and graphing functions in Matlab
Functional programming:
Using functions to achieve complex tasks
Functions may be Matlab in-built or user constructed
User defined functions are used to perform repetitive tasks
Examples:
size provides the length and breadth of a 2-D array or a matrix
[row,columns] = size(A)
variable row has the number of rows in A
variable columns has the number of columns in A
Note: A program in Matlab is a collection of statements which may or may not comprise
functions
Graphing data in Matlab:
Function Name Usage Use
plot plot(xdata,ydata,style,color) To plot a single line graph in a new
window
subplot subplot(rows, columns, axis Use with plot to graph multiple figures
position) in the same window in a tabular format
hold hold on (or) hold off Use with plot to draw multiple graphs
with the same axis in the same window
figure figure(handle) (or) figure To open a new figure window or set the
existing figure window as current
Note: style and color refer to the style of the line and its color on the plot in case of line plots
Note: axis position is counted column wise in each row for subplot
Formatting options:
Function Name Usage Use
xlabel xlabel(‘xlabel’) To specify a x-axis label
ylabel ylabel(‘ylabel’) To specify a y-axis label
axis axis([xlow xhigh ylow yhigh]) To specify limits on the axes
title title(‘title string’) To specify a figure title
Note: All formatting is applied to the current axis in the current figure
References:
Matlab help documentation
Module 1, pages 539-557, Process control, modeling, design and simulation by Wayne Bequette