Variable name of function input
inputname
is a convenient way
to communicate the name of an input variable to a function. For example,
a function checks the data types of inputs and, if it finds an incorrect
type, displays the name of the variable from your workspace.
Avoid using inputname
in the
critical path of code or to obtain variable names to be used with
commands such as eval
, evalin
,
and assignin
. This use of inputname
can
lead to code that is difficult to maintain.
inputname
returns an error if it is called inside an
overloaded subsref
, subsasgn
,
subsindex
, numArgumentsFromSubscript
,
numel
, or property set or get method.
If the function that calls inputname
is
not called from a MATLAB code file, inputname
walks
up the stack until it finds a call from MATLAB code and returns
the names it finds there. For example, this behavior occurs if inputname
is
called from a built-in function or a MEX function.
Consider the following code in which the built-in arrayfun
function
calls inputname
via a function handle.
fn=@(x) inputname(x);
a=1:4;
arrayfun(fn,a,'uniformoutput',false)
ans = 'fn' 'a' '' ''
The inputname
function walks up the stack
until it finds a call from MATLAB code. In this case, the MATLAB code
is the base workspace, and inputname
returns
variable names from the base workspace