SOME MATLAB CONCEPTS 

Maarten van Walstijn 

PBASS 
MATLAB CONCEPTS 
1 

SCRIPTING , EDITING, EXECUTING 

<file.m> 
File.m is a ‘Matlab script’: 

command 1 
Contains a series of commands (command lines) that execute a particular set of instructions. 

command 2 

…. 

…. 
Can be created & edited with the Matlab editor. 


Go to within working directory 

Store in ‘work’ folder 

Execute by typing filename Into Matlab comandline 

>> 

>> 

>> file 

PBASS 
MATLAB CONCEPTS 
2 
Vectorisation (2) 

When using vectorised operations, one must in some cases use 

A 
period ‘.’ before the normal operation. For example, 

>> z = x.*y 

z 
= 

5 
8 
9 
8 
5 

>> z = x./y 

z 
= 

0.2000 
0.5000 
1.0000 
2.0000 
5.0000 

>> 

PBASS 
MATLAB CONCEPTS 
5 

‘ForLoops’ 

A 
series of commands of the same format is usually code with a 

socalled ‘forloop’. For example, we could have done the 

previous exercise as follows: 

N 
= 5; 

x 
= 1:N; 

y 
= N:1:1; 

z 
= zeros(1,N); 
% creating a new vector to store the result in 

%%% forloop %%% for n=1:N 

z(n) = x(n) + y(n); 

end 

Often it is not efficient to use a fotloop, as it requires more code and runs slower!. 

One case in which we always need a foloop is when the elements of the resultvector need to be computed recursively. 

PBASS 
MATLAB CONCEPTS 
6 
SOME PLOTTING TRICKS (1) 

Different plots can be realised within one figure. For example: 

plot(x1,y1,’b’,x2,y2,’ro’) 

Plots two sets of xy data: the first with a blue solid line, and the second with a dashed red line plus a red circle at each data point. The same can also be realised with: 

plot(x1,y1,’b’) 

hold on; 

plot(x2,y2,’ro’) 

hold off; 

See ‘help plot’ for more details and options. 

PBASS 
MATLAB CONCEPTS 
9 

SOME PLOTTING TRICKS (1) 

The properties of the axes often need to be specified. Here’s a few options: 

axis off; 
> makes all axes disappear (data only display) 

axis square; 
> forces a ‘square’ shape to the axes 

axis([xmin xmax ymin ymax]); 
> sets the ‘view’ to specified x an y ranges. 

See ‘help axis’ for more details & options. 

PBASS 
MATLAB CONCEPTS 
10 
Much more than documents.
Discover everything Scribd has to offer, including books and audiobooks from major publishers.
Cancel anytime.