Professional Documents
Culture Documents
with MATLAB
By
Yap Shook Chin
Working with the Desktop
Modifying the desktop
layout
Sub-Windows:
Left upper: Workspace,
Launch Pad, and
Current directory
Left lower: Command
History
Right: Command
Window
Modifying the Desktop Layout
Changing the number and sizes of the sub-
windows
Docking/undocking the displayed sub-
windows
Setting environment preference
Font, color, numeric format, etc.
Search Path – directories that MATLAB
searches through
Current Directory field
Getting Help
Help Æ Matlab Help
Launch Pad Æ Help
Command window: >> help …
>> lookfor …
MATLAB Demos
Workspace and Array Editor
Workspace consists of variables currently
held in memory (name, nature, size)
Variables can be saved and open in the
.mat file format using command save and
load respectively.
Double click a variable to view its content in
array editor
Special Values
There are some important variables and
constants used in MATLAB programming.
Matlab as a Simple Calculator
>> (-1+2+3)*5 -2/3
>> 2^3
>> cos (pi/2)
>> p = 2+3;
>> q = 3+5;
>> ratio = p/q
Ratio = 0.6250
>> x = 2+ 3*i
>> abs(x)
>> angle(x)
Arithmetic
Operators
>> A = [2 3 4]
>> B = [1 -2 -3]
>> A.*B
ans = [2 -6 -12]
>> A*B’
ans = -16
>> A’ *B
Ans = 2 -4 -6
3 -6 -9
4 -8 -12
Relational Operators
>> A = [1 2 3 4 5 6]
>> B = [0 2 3 6 5 7]
>> A == B
ans = [0 1 1 0 1 0]
>> A >= B
ans = [1 1 1 0 1 0]
Logical Operators
>> A = [1 2 0]
>> B = [1 -2 3]
>> A & B
ans = [1 1 0]
>> xor(A,B)
Ans = [1 0 0]
Working with Matrix/Array
Vector indexing
Syntax : >> array_name(row, col)
>> a = [2 4 9 4 6];
>> a(3)
ans = 9
>> a(2:4)
ans = 4 9 4
>>a(3:end)
Ans = 9 4 6
>> B = A( : ,3)
B=
3
6
9
>> C = A(2, :)
C=
4 5 6
Working with Matrix/Array
Matrix indexing
>> A = [1 2 3; 4 5 6; 7 8 9]
>> E = A;
>> E( : , 3) =0
E=
1 2 0
4 5 0
7 8 0
>> N = [1,2,3];
>> M = [N, 6:8]
Ans = [1,2,3,6,7,8]
Some Important Standard Array
zeros(M,N) generates an M x N of 0s of class double
ones(M,N) generates an M x N of 1s of class double
true(M,N) generates an M x N logical matrix of 1s.
false(M,N) generates an M x N logical matrix of 0s.
Rand(M,N) generates an M x N matrix whose entries are
uniformly distributed random numbers in the interval [0,1]
>> S = 7* ones(2,3)
S=
7 7 7
7 7 7
Plotting
Linear Plotting
>> x = 0:0.1:2*pi;
>> y = sin(x);
>> plot(x,y)
>> plot(y)
>> plot(x,y,’y’)
>> plot(x,y,’g*’)
>> title(‘Sinosoidal Graph’)
Plotting
Adding a grid
>> grid on
Deleting/copying the
current object
Graph annotation
Programming in Matlab
Flow control
Functions
Variables and workspace
Subfunctions
Debugging
Flow Control
Logic control:
If / elseif / else
Switch / case / otherwise
Iterative loops:
for
while
Functions
Three type of Functions:
Core MATLAB (built-in) functions, e.g. sin, abs,…
MATLAB-supplied m-file functions, e.g. mead, std,…
User-created m-file functions
Two types: script and function
Call function
>> output_value = AverageArray([1,2,3])
Function M-file
The function definition line
function [av] = AverageArray (x)
The H1 line % this function calculate the average
of the output from an equation
% where x = input as an 1D vector
Help text % y = output as an 1D vector
% n = size of vector x
% av = average of the output y
n = size(x);
The function body
y = 5*x + 8;
av = sum(y)/n(2);
comments
Variables and Workspace
Variables are stored in workspaces
MATLAB workspace
For command line and script file variables
Function workspace
Each function has its own workspace for local
variables.
Access via inputs and outputs.
Global workspace
Global variables can be shared by multiple
workspace
Must be initialized in all relevant workspace
Subfunctions
Subfunctions can only be called from within
the same m-file
Each subfunction has its own workspace
Allows more than one function to be within
the same m-file (modularize code)
M-files containing subfunctions must have
the name of the first (primary) function.
MATLAB Calling Priority
High
Variable
built-in function
subfunction
private function
Mex-file
P-file
M-file
low
becomes
>> x = 0:0.01:2;
>> y = sin(x*pi);
Preallocating Arrays
Preallocate the size of the arrays used in a
program to improve code execution time and
reduce memory fragmentation when working with
large arrays.