Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

inputname

Variable name of function input

Syntax

Description

example

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.

Examples

collapse all

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

function myfun(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;
myfun(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.

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

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

function myfun2(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);

myfun2(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.

myfun2(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.

myfun2(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.

More About

collapse all

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, or numel 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

See Also

| |

Introduced before R2006a

Was this topic helpful?