1 / 20

To days Outline

To days Outline. Functions Strings Sparse Arrays Cell Arrays Structures Exercises on this days topics. Functions files. Runs in its own independent workspace Receives input data through an Input argument list Returns data through an Output argument list. Functions files.

ingrid-pate
Download Presentation

To days Outline

An Image/Link below is provided (as is) to download presentation 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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. To days Outline • Functions • Strings • Sparse Arrays • Cell Arrays • Structures • Exercises on this days topics lecture 3

  2. Functions files • Runs in its own independent workspace • Receives input data through an • Input argument list • Returns data through an • Output argument list lecture 3

  3. Functions files • The first word written in the file must bethe word function • function [outarg1,…] = fname(inarg1,…) %Comment lines … (Executable code) … (return) lecture 3

  4. 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. lecture 3

  5. Example • Create a function that adds a value to the input argument and returns the new value. lecture 3

  6. 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 lecture 3

  7. 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? lecture 3

  8. 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 • nargchk Returns a standard error message if a function is called with to few or to many arguments lecture 3

  9. 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 lecture 3

  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. Functions files • Global Memory • Provides a way to share data between functions • A global memory is declared with the global statement. • global var1 var2 … lecture 3

  12. 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 … lecture 3

  13. 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 lecture 3

  14. 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 lecture 3

  15. 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 lecture 3

  16. 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) lecture 3

  17. Structures • Structures can be created in two ways • 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 lecture 3

  18. 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 lecture 3

  19. Structures • 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. lecture 3

  20. 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). lecture 3

More Related