Matlab: Presentation By: AMU ROBOCLUB
Matlab: Presentation By: AMU ROBOCLUB
x y
Workspace(Cont..)
Can manipulate variables stored in the
workspace
>> b=10;
>> c=a+b
c=
22
>>
Variables
Dont have to declare type
Dont even have to initialise
Just assign in command window
>>
>> a=12; % variable a is assigned 12
Matlab comment
prompt operator
suppress
assign
command
operator
output
Variables (Cont..)
View variable contents by simply typing the
variable name at the command prompt
>> a
a=
12
>>
>> a*2
a=
24
>>
Matrices
Dont need to initialise type, or dimensions
>>A = [3 2 1; 5 1 0; 2 1 7]
A=
3 2 1
5 1 0
2 1 7
>>
Manipulating Matrices
Access elements of a matrix
A=
3 2 1
5 1 0
>>A(1,2) 2 1 7
ans=
2
Remember Matrix(row, column)
Naming convention Matrix variables start
with a capital letter while vectors or scalar
variables start with a simple letter
Basic Operators
+, -, *, / : basic numeric operators
\ : left division (matrix division)
^ : raise to power
: transpose (of matrix) flip along
diagonal
fliplr(), flipud() : flip matrix about
vertical and horizontal axes.
The : Operator
VERY important operator in Matlab
Means to
>> 1:10
ans =
1 2 3 4 5 6 7 8 9 10
>> 1:2:10
ans =
1 3 5 7 9
The : Operator and Matrices
>>A(3,2:3) A=
3 2 1
ans = 5 1 0
2 1 7
1 7
>>A(:,2)
ans =
2
1
1
Manipulating Matrices A=
3 2 1
>> A ' % transpose 5 1 0
2 1 7
>> B*A % matrix multiplication
>> B.*A % element by element multiplication
>> B/A % matrix division B=
1 3 1
>> B./A % element by element division 4 9 5
>> [B A] % Join matrices (horizontally) 2 7 2
Matlab Desktop
Press to create new
m-file in the matlab
editor
Scripts(Cont..)
Scripts will manipulate and store variables and matrices
in the Matlab Workspace (memory).
They can be called from the Matlab command line by
typing the (case sensitive!) filename of the script file.
>> myscript
Scripts can be opened in the editor by the following
>> open myscript
Saving Data
MATLAB uses its own platform independent file
format for saving data files have a .mat
extension
The save command saves variables from
the workspace to a named file (or matlab.mat
if no filename given)
save filename saves entire workspace to
filename.mat
save var1 var2 filename saves named
variables to filename.mat
Saving Data(Cont..)