Main Content

inputname

Variable name of function input

Description

s = inputname(argNumber) returns the workspace variable name, s, corresponding to the argument number argNumber.

You cannot call inputname from the MATLAB® command prompt or in a script you call from the command prompt.

example

Examples

collapse all

Create the following function in a file, getname.m, in your current working folder.

function getname(a,b)
s = inputname(1);
disp(['First calling variable is ''' s '''.'])
end

Call the function at the command prompt using the variables x and y.

x = 5;
y = 3;
getname(x,y)
First calling variable is 'x'.

Call the function using values instead of variables. The inputname function returns an empty char array because its input does not have a name.

getname(5,3)
First calling variable is ''.

Create the following function in a file, getname2.m, in your current working folder.

function getname2(a,b,c)
for m = 1:nargin
    disp(['Calling variable ' num2str(m) ' is ''' inputname(m) '''.'])
end

Call the function at the command prompt.

x = {'hello','goodbye'};
y = struct('a',42,'b',78);
z = rand(4);

getname2(x,y,z)
Calling variable 1 is 'x'.
Calling variable 2 is 'y'.
Calling variable 3 is 'z'.

Call the function using a field of y. Because the input argument contains dot indexing, the inputname function returns an empty char array for the second variable name and all subsequent variable names.

getname2(x,y.a,z)
Calling variable 1 is 'x'.
Calling variable 2 is ''.
Calling variable 3 is ''.

Call the function using the second cell of x. Because the input argument contains cell indexing, the inputname function returns an empty char array for the first variable name and all subsequent variable names.

getname2(x{2},y,z)
Calling variable 1 is ''.
Calling variable 2 is ''.
Calling variable 3 is ''.

Input Arguments

collapse all

Number of function input argument, specified as a scalar, real, positive integer value. If argNumber exceeds the number of input arguments passed into the function, MATLAB throws an error.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Output Arguments

collapse all

Workspace variable name, returned as a character vector. If the input argument has no name, the inputname function returns an empty character array (''). For example, an input argument has no name if it is a number, an expression, or an indexing expression instead of a variable.

If an input argument to the function that calls inputname contains cell or dot indexing, inputname returns an empty character array for that variable and subsequent variables. Extracting elements from a cell array or a field from a structure yields a comma-separated list. A comma-separated list causes the location of any input that follows to be dynamic. Therefore, inputname returns '' for the argument that produced the list and the arguments that follow.

Tips

  • 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.

  • inputname cannot get the names of arguments that are contained in a forwarded indexing operation. Instead, it returns an empty string.

  • 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

Extended Capabilities

Version History

Introduced before R2006a