MATLAB Programming

MATLAB Programming MATLAB Programming

cda.psych.uiuc.edu
from cda.psych.uiuc.edu More from this publisher
23.06.2015 Views

4 M-File Programming Using nargin and nargout When nargin or nargout appears in a nested function, it refers to the number of inputs or outputs passed to that particular function, regardless of whether ornotitisnested. In the example shown above, nargin in function A is the number of inputs passed to A, andnargin in function C is the number of inputs passed to C. Ifa nested function needs the value of nargin or nargout from an outer function, youcanpassthisvalueinasaseparateargument,asdoneinfunctionB. Example of Passing Optional Arguments to Nested Functions This example references the primary function’s varargin cell array from each of two nested functions. (Because the workspace of an outer function is shared with all functions nested within it, there is no need to pass varargin to the nested functions.) Both nested functions make use of the nargin value that applies to the primary function. Calling nargin from the nested function would return the number of inputs passed to that nested function, and not those that had been passed to the primary. For this reason, the primary function must pass its nargin value to the nested functions. function meters = convert2meters(miles, varargin) % Converts MILES (plus optional FEET and INCHES input) % values to METERS. if nargin < 1 || nargin > 3 error('1 to 3 input arguments are required'); end function feet = convert2Feet(argsIn) % Nested function that converts miles to feet and adds in % optional FEET argument. feet = miles .* 5280; if argsIn >= 2 feet = feet + varargin{1}; end 4-50

Function Arguments end % End nested function convert2Feet function inches = convert2Inches(argsIn) % Nested function that converts feet to inches and adds in % optional INCHES argument. inches = feet .* 12; if argsIn == 3 inches = inches + varargin{2}; end end % End nested function convert2Inches feet = convert2Feet(nargin); inches = convert2Inches(nargin); meters = inches .* 2.54 ./ 100; end % End primary function convert2meters convert2meters(5) ans = 8.0467e+003 convert2meters(5, 2000, 4.7) ans = 8.6564e+003 Returning Modified Input Arguments If you pass any input variables that the function can modify, you will need to include the same variables as output arguments so that the caller receives the updated value. For example, if the function readText, shown below, reads one line of a file each time is it called, then it must keep track of the offset into the file. But when readText terminates, its copy of the offset variable is cleared from memory. To keep the offset value from being lost, readText must return this value to the caller: function [text, offset] = readText(filestart, offset) 4-51

Function Arguments<br />

end<br />

% End nested function convert2Feet<br />

function inches = convert2Inches(argsIn)<br />

% Nested function that converts feet to inches and adds in<br />

% optional INCHES argument.<br />

inches = feet .* 12;<br />

if argsIn == 3<br />

inches = inches + varargin{2};<br />

end<br />

end % End nested function convert2Inches<br />

feet = convert2Feet(nargin);<br />

inches = convert2Inches(nargin);<br />

meters = inches .* 2.54 ./ 100;<br />

end % End primary function convert2meters<br />

convert2meters(5)<br />

ans =<br />

8.0467e+003<br />

convert2meters(5, 2000, 4.7)<br />

ans =<br />

8.6564e+003<br />

Returning Modified Input Arguments<br />

If you pass any input variables that the function can modify, you will need to<br />

include the same variables as output arguments so that the caller receives<br />

the updated value.<br />

For example, if the function readText, shown below, reads one line of a file<br />

each time is it called, then it must keep track of the offset into the file. But<br />

when readText terminates, its copy of the offset variable is cleared from<br />

memory. To keep the offset value from being lost, readText must return this<br />

value to the caller:<br />

function [text, offset] = readText(filestart, offset)<br />

4-51

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!