User-defined Functions (11/28) Optional Arguments Optional arguments can be checked using: nargin:number of input arguments nargout:number of output arguments nargchk:validates number of arguments error:display error message and abort the function producing the error warning:display warning message and continue function execution inputname:return the actual name of the variable that corresponds to a particular argument number 同停大学 TONGJI UNIVERSITY
Optional arguments can be checked using: ✓ nargin: number of input arguments ✓ nargout: number of output arguments ✓ nargchk: validates number of arguments ✓ error: display error message and abort the function producing the error ✓ warning: display warning message and continue function execution ✓ inputname: return the actual name of the variable that corresponds to a particular argument number User-defined Functions (11/28) Optional Arguments
User-defined Functions (12/28) Optional Arguments Example We will illustrate the use of optional arguments by creating a function that accepts an(x,y)value in rectangular coordinates and produces the equivalent polar representation consisting of a magnitude and an angle in degrees. function [mag angle]=polar_value(x,y) %POLLAR VALUE Converts(x,y)to (r,theta) %function polar_value converts an input (x,y) %value into(r,theta),with theta in degrees. %It illustrates the use of optional arguments. %Define variables: angle---angle in degrees %msg---ERROR message mag---magnitude x---input x value @日济大学 y---input y value (optional) TONGJI UNIVERSITY
Example We will illustrate the use of optional arguments by creating a function that accepts an(x,y) value in rectangular coordinates and produces the equivalent polar representation consisting of a magnitude and an angle in degrees. function [mag,angle]=polar_value(x,y) %POLLAR_VALUE Converts (x,y) to (r,theta) %function polar_value converts an input (x,y) %value into (r,theta),with theta in degrees. %It illustrates the use of optional arguments. %Define variables: % angle--- angle in degrees % msg--- ERROR message % mag--- magnitude % x--- input x value % y--- input y value (optional) User-defined Functions (12/28) Optional Arguments