From CADDementia
Jump to: navigation, search
%% Functions with Multiple Inputs or Outputs
% Anonymous functions require that you explicitly specify the input
% arguments as you would for a standard function, separating multiple
% inputs with commas. For example, this function accepts two inputs,
% |x| and |y|:
myfunction = @(x,y) (x^2 + y^2 + x*y);
x = 1;
y = 10;
z = myfunction(x,y)
% However, you do not explicitly define output arguments when you create an
% anonymous function. If the expression in the function returns multiple
% outputs, then you can request them when you call the function. Enclose
% multiple output variables in square brackets.
% For example, the |ndgrid| function can return as many outputs as the
% number of input vectors. This anonymous function that calls |ndgrid| can
% also return multiple outputs:
c = 10;
mygrid = @(x,y) ndgrid((-x:x/c:x),(-y:y/c:y));
[x,y] = mygrid(pi,2*pi);
% You can use the output from |mygrid| to create a mesh or surface plot:
z = sin(x) + cos(y);
% Copyright 2014, The MathWorks Inc.