inputname

Returns the name of the variable passed as a given argument of the current function.

Category: Programming > Functions

Syntax

name = inputname(argNumber)

Returns the variable name used for argument argNumber.

name = inputname(argNumber, ids_only)

The optional flag ids_only controls what is returned when the argument is not a plain variable.

Description

A function normally sees only values. inputname lets it recover the name the caller used, which is useful for labeling output, as in disp([inputname(1) ' = ' num2str(x)]). When the argument was not a plain variable, the result is empty or, with ids_only set to false, the text of the expression. It can be called only from inside a function.

The example below writes a function show to a file; in MATLAB it prints [alpha].

Input arguments

argNumber
Position of the argument, counted from 1.
ids_only
Logical, optional. Selects whether only variable names are accepted (default true).

Output arguments

name
Character vector with the caller's variable name, or empty.

Example

Naming the arguments

writelines(["function show(a)", "  fprintf('[%s]\n', inputname(1));", "end"], 'show.m');
alpha = 1;
show(alpha)
Output
[]

MatLivre currently returns an empty name here, so it prints [].

Notes

In MatLivre inputname always returns an empty character vector, whatever the argument. Pass the name explicitly when a function needs it.

See also

evalin assignin inputParser