23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Function Arguments<br />

nargin and nargout, on the other hand, are functions and when called within<br />

a nested function, always return the number of arguments passed to or from<br />

the nested function itself.<br />

Using varargin and varargout<br />

varargin or varargout used in a nested function can refer to optional<br />

arguments passed to or from that function, or to optional arguments passed<br />

to or from an outer function.<br />

• If a nested function includes varargin or varargout in its function<br />

declaration line, then the use of varargin or varargout within that<br />

function returns optional arguments passed to or from that function.<br />

• If varargin or varargout are not in the nested function declaration but<br />

are in the declaration of an outer function, then the use of varargin or<br />

varargout within the nested function returns optional arguments passed<br />

to the outer function.<br />

In the example below, function C is nested within function B, and function B is<br />

nested within function A. Thetermvarargin{1} in function B refers to the<br />

second input passed to the primary function A, whilevarargin{1} in function<br />

C refers to the first argument, z, passed from function B:<br />

function x = A(y, varargin)<br />

B(nargin, y * rand(4))<br />

% Primary function A<br />

function B(argsIn, z) % Nested function B<br />

if argsIn >= 2<br />

C(z, varargin{1}, 4.512, 1.729)<br />

end<br />

function C(varargin) % Nested function C<br />

if nargin >= 2<br />

x = varargin{1}<br />

end<br />

end % End nested function C<br />

end % End nested function B<br />

end % End primary function A<br />

4-49

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

Saved successfully!

Ooh no, something went wrong!