INTRODUCTION TO MATLAB Part 2 . UNIVERSITY OF SHEFFIELD CiCS DEPARTMENT Deniz Savas & Mike Griffiths MAY.2011. Outline of Part 2. Writing matlab functions and scripts Relational Operations Flow Control statements Basic matlab graphics. Matlab Scripts and Functions.
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
function return_vars = name ( input_args )
Matlab functions can return no value or many values, each of the values being scalars or general matrices.
If a function returns no value the format is;
function name ( input_args)
If the function returns multiple values the format is;
function [a,b,c ] = name ( input_args )
If the function name and the filename ‘where the function resides are not the same the filename overrides the function name.
Example : pcode myfunc
< less than
<= less than or equal to
> greater than
>= greater than or equal to
= = equal to
~ = not equal to
Result is a scalar, vector or matrix of of same size and shape whose each element contain only 1 ( to imply TRUE) or 0 (to imply FALSE ) .
A = magic(6) ; A is 6x6 matrix of magic numbers.
P = ( rem( A,3) = = 0 ) ; Elements of P divisible by 3 are set to 1
while others will be 0.
A = rand(12,1) ;---> a vector of random no.s
p = a >= 0.7 ;----> p will be 1’s and 0’s .
b = A(find(p>0))=0.5;----> only the elements of A which
are >=0.7 will be set to 0.5.
for v = expression
where expression is an array or matrix.
If it is an array expression then each element is assigned
one by one to v and loop repeated.
If matrix expression then each column is
assigned to v and the loop repeated.
sum = 0.0
for v = 1:5
sum = sum + 1.0/v
below example evaluates the loop with
v set to 1,3,5, …, 11
for v = 1:2:11
n = 1
while prod(1:n) < 1E100
n= n + 1
Perform Exercises 5b on the Exercises Sheet
This statement passes control to the next iteration of the for or while loop in which it appears, skipping any remaining statements in the body of the loop.
Execute only one block from a selection of blocks of code according to the value of a controlling expression.
method = 'Bilinear';
% note: lower converts string to lower-case.
disp('Method is linear')
disp('Method is cubic')
disp('Method is nearest')
Note: only the first matching case executes, ‘i.e. control does not drop through’.
Example: height=‘h’ ; s = ‘my title’ ;
For example: s(4:8) will return ‘title’ .
title=‘my title’ ;
double(title) > will display : 109 121 116 105 116 108 101
grav = 9.81;
gtitle1 = num2str(grav) > will create string gtitle1=‘9.81’
gtitle2 = sprintf(‘%f ’ , grav) > will create string gtitle2=‘9.8100’
A= [65:75] ; char(A) > will display ABCDEFGHIJK.
Now try : A= A+8 ; char(A) What do you see ?
achieved by the use of the input function:
n = input ( ‘Enter a number:’ );
will prompt for a number and read it into the variable named n .
echo on or echo off
pause or pause (n) for n-seconds pause
the colour indicator is one of c,m,y,r,g,b or w.
t = 0:0.1:2*pi;
a = [ sin(t) ; cos(t) .*exp(-t) ] ;
a = a’;
Note: dot between cos(t) exp(-t)
fplot( 'sin(x).*exp(-x/4)' ,[ 1 10 ] )
Figure : This command creates a new graphics window. Multiple figure windows will happily coexist. Only one of the figures will have the focus for drawing.
Hold : Each graph is usually drawn into a clean window, by deleting the current display area. The hold command will over-ride this behaviour and draw the new graph over the existing one.
Subplot : This command will divide the current graphics figure into multiple panels and allow you to draw a different graphs in each panel.
Example: subplot(2,2,1) ; plot(y1)
subplot(2,2,2) ; plot(y2)
subplot(2,2,3) ; plot(y3)Plot control commands
Save the statistics to the workspace and access them