To days Outline

advertisement
To days Outline






Functions
Strings
Sparse Arrays
Cell Arrays
Structures
Exercises on this days topics
MATLAB and Simulink
lecture 3
1
Functions files


Runs in its own independent workspace
Receives input data through an


Input argument list
Returns data through an

Output argument list
MATLAB and Simulink
lecture 3
2
Functions files


The first word written in the file must be the
word function
function [outarg1,…] = fname(inarg1,…)
%Comment lines
…
(Executable code)
…
(return)
MATLAB and Simulink
lecture 3
3
Functions files

Variable passing

When a function calls occurs.
 MATLAB makes a copy of the arguments
 If the function modifies the input
argument it will not affect the original
data in the caller.
 This apply both scalars and arrays.
MATLAB and Simulink
lecture 3
4
Example

Create a function that adds a value to
the input argument and returns the
new value.
MATLAB and Simulink
lecture 3
5
Example
In command window
>> A=[1 2];
>> B=myfun(A);
Input argument before modifying: [1 2]
Input argument after modifying: [11 12]
>> disp(A)
1
2
>> disp(B)
11 12
MATLAB and Simulink
lecture 3
6
Functions files

Optional Arguments




Many function supports optional input and
output arguments
We have used the plot command with several
different Input argument lists.
The max command accepts different output
arguments
How do MATLAB functions know how many
arguments are present and how do they
adjust their behavior?
MATLAB and Simulink
lecture 3
7
Functions files

MATLAB offers some special functions to get information
about the arguments and to report errors
 nargin
Returns the actual number of input
arguments used to call the function.

nargout

error(msg)

warning(msg)

nargchk
MATLAB and Simulink
Returns the actual number of output
arguments used to call the function.
Display error message and abort
the function producing the error.
Display warning message
Execution can continue
Returns a standard error message if a
function is called with to few or to many
arguments
lecture 3
8
Example
The location of a point in the Cartesian plane can be
represented either in rectangular (x,y) or polar coordinates
(r,θ).
x  r  cos( )
y  r  sin(  )
•
•
•
•
r  x2  y 2
  tan 1 y
x
Write a function rect2polar that converts rectangular coordinates to polar
coordinates.
The function is design to support two input arguments (x,y)
However if only one input argument is supplied the functions should
suppose that y is equal to zero.
The function normally returns both magnitude and angle but if only one
output argument is present, it returns only the magnitude
MATLAB and Simulink
lecture 3
9
Test the function: In command window:
>> [mag,angle]=rect2polar
??? Error using ==> rect2polar
Not enough input arguments.
>> [mag,angle]=rect2polar(1,-1,2)
??? Error using ==> rect2polar
Too many input arguments.
>> [mag,angle]=rect2polar(1)
mag =
1
angle =
0
>> [mag,angle]=rect2polar(1,1)
mag =
1.4142
angle =
45
>> [mag]=rect2polar(1,1)
mag =
1.4142
Functions files

Global Memory


Provides a way to share data between
functions
A global memory is declared with the
global statement.

global var1 var2 …
MATLAB and Simulink
lecture 3
11
Functions files

Preserving data


When a function finishing executing the
special workspace will be destroyed.
Persistent memory


Only accessed within the function
A persistent variable is declared using the
persistent statement

persistent var1 var2 …
MATLAB and Simulink
lecture 3
12
Functions files




Subfunctions or Internal functions
More than one function in a single file
Top function is the normal function
The ones below are subfunctions or
Internal functions
MATLAB and Simulink
lecture 3
13
Strings

In MATLAB a string is an array of type char.


Creating two-dimensional Character arrays


Each row must have the same length
MATLAB offers a number of string operations


str=’This Is a string’
strcmp(str1,str2)
Compares two strings
Returns 1 if true.
See MATLAB help for more string operations
MATLAB and Simulink
lecture 3
14
Sparse Arrays


Sparse arrays are special arrays in
which memory is only allocated for the
nonzero elements.
using sparse arrays saves memory and
can make a program run faster.

sparse(A)
MATLAB and Simulink
Converts A to a sparse
array
lecture 3
15
Cell Arrays

A cell array is a special array that can
contain different data:





Strings
Complex numbers
vectors
Cell array uses braces {} instead of
parentheses () for selecting cells
To view a cell array graphically use
cellplot(A)
MATLAB and Simulink
lecture 3
16
Structures
 Structures can be created in two ways
1.
A field at a time using assignment statements.
>>student.name='Carl';
>>student.addr='Borlänge';
>>student.phone=778858;
student =
name: 'Carl'
addr: 'Borlänge'
phone: 778858
MATLAB and Simulink
lecture 3
17
Structures
A second student can be added to the
structure by using a subscript
>>student(2).name=’John’;
 The other field will automatically be
initialized as an empty array.
 Accessing field in the structure using
parentheses

MATLAB and Simulink
lecture 3
18
Structures
2.


All at once using the struct function
str_array=struct(field1,val1,field2,val2)
Example:
student=struct('Name',{'Carl','John'},'Addr',...
'Borlänge','Falun'},'Phone',{778858,123456})

This will construct a 1x2 structure.
MATLAB and Simulink
lecture 3
19
Exercises on this days topics



5.1 , 5.2, 5.10(8) , 5.16(13) ,
5.19(16), 5.24(20)
6.1 , 6.20(18)
7.6

The exercise numbers are referring
to the book MATLAB Programming
for engineers, third edition
20(second edition).
MATLAB and Simulink
lecture 3
20
Download