Download presentation
Presentation is loading. Please wait.
Published byMichael Baker Modified over 10 years ago
1
MATLAB and Simulinklecture 31 To days Outline Functions Strings Sparse Arrays Cell Arrays Structures Exercises on this days topics
2
MATLAB and Simulinklecture 32 Functions files Runs in its own independent workspace Receives input data through an Input argument list Returns data through an Output argument list
3
MATLAB and Simulinklecture 33 Functions files The first word written in the file must be the word function function [outarg1,…] = fname(inarg1,…) %Comment lines … (Executable code) … (return)
4
MATLAB and Simulinklecture 34 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.
5
MATLAB and Simulinklecture 35 Example Create a function that adds a value to the input argument and returns the new value.
6
MATLAB and Simulinklecture 36 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
7
MATLAB and Simulinklecture 37 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?
8
MATLAB and Simulinklecture 38 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 Returns the actual number of output arguments used to call the function. error(msg)Display error message and abort the function producing the error. warning(msg)Display warning message Execution can continue nargchkReturns a standard error message if a function is called with to few or to many arguments
9
MATLAB and Simulinklecture 39 Example The location of a point in the Cartesian plane can be represented either in rectangular (x,y) or polar coordinates (r,θ). 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
10
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
11
MATLAB and Simulinklecture 311 Functions files Global Memory Provides a way to share data between functions A global memory is declared with the global statement. global var1 var2 …
12
MATLAB and Simulinklecture 312 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 …
13
MATLAB and Simulinklecture 313 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
14
MATLAB and Simulinklecture 314 Strings In MATLAB a string is an array of type char. str=’This Is a string’ Creating two-dimensional Character arrays Each row must have the same length MATLAB offers a number of string operations strcmp(str1,str2)Compares two strings Returns 1 if true. See MATLAB help for more string operations
15
MATLAB and Simulinklecture 315 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)Converts A to a sparse array
16
MATLAB and Simulinklecture 316 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)
17
MATLAB and Simulinklecture 317 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
18
MATLAB and Simulinklecture 318 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
19
MATLAB and Simulinklecture 319 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.
20
MATLAB and Simulinklecture 320 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).
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.