args, a MATLAB code which demonstrates command line argument retrieval.
In particular, this is a demonstration of the NARGIN function, which counts command line arguments and the VARARGIN function, which allows the user to specify that a routine has a variable number of input arguments.
If a MATLAB function takes input arguments, such as
function value = summit ( alpha, beta )then there are two ways of supplying some or all the argument values when invoking the function.
We can call with commandline arguments:
value = summit or value = summit 2 or value = summit 2 4or with functional arguments:
value = summit ( ) or value = summit ( 2 ) or value = summit ( 2, 4 )as long as the arguments are processed correctly.
In the commandline form, the arguments will be given to the function as character strings, which must be re-interpreted as numbers. A convenient way to allow the user to use either way of invoking the function requires that the function include a check as to the type of value it has been given. For example, to handle ALPHA, we might write
if ( nargin < 1 ) alpha = input ( 'Enter value of alpha: ' ); elseif ( ischar ( alpha ) ) alpha = str2num ( alpha ); end
The computer code and data files made available on this web page are distributed under the MIT license
args is available in a C version and a C++ version and a FORTRAN90 version and a MATLAB version and a Python version.