Graphics Programs Reference
In-Depth Information
As an example, here we use
isnumeric
in the M-file
add.m
to print an
error message if any of the inputs are not numeric:
function s = add(varargin)
if ~isnumeric([varargin{:}])
error('Inputs must be floating point numbers.')
end
s = sum([varargin{:}]);
WhenafunctionM-fileallowsmultipleoutputarguments,theniffewerout-
putargumentsarespecifiedwhenthefunctioniscalled,theremainingoutputs
aresimplynotassigned.Recallthatifnooutputargumentsareexplicitlyspec-
ified on the command line, then a single output is returned and assigned to
the variable
ans
. For example, consider the following M-file
rectangular.m
that changes coordinates from polar to rectangular:
function [x, y] = rectangular(r, theta)
x = r.*cos(theta);
y = r.*sin(theta);
If you type
rectangular(2, 1)
at the command line, then the answer will
be just the
x
coordinate of the point with polar coordinates (2
,
1). The following
modificationto
rectangular.m
adjuststheoutputinthiscasetobeacomplex
number
x
+
iy
containing bothcoordinates:
function [x, y] = rectangular(r, theta)
x = r.*cos(theta);
y = r.*sin(theta);
if nargout < 2
x=x+i*y;
end
See the online help for
varargout
and the functions described above for ad-
ditional information and examples.
User Input and Screen Output
In the previous section we used
error
to print a message to the screen and
then terminate execution of an M-file. You can also print messages to the
screen without stopping execution of the M-file with
disp
or
warning
. Not
surprisingly,
warning
is intended to be used for warning messages, when
the M-file detects a problem that might affect the validity of its result but is
not necessarily serious. You can suppress warning messages, either from the
Search WWH ::
Custom Search