YLM
YLM

Reputation: 345

Use the name of a variable in the title of a plot

I would like to use the name of a variable in the title of a plot. I tried this :

str = sprintf('Spectral analysis - WELCH - %d', X);

But this take the values of X, instead of X itself.

Any ideas?

Upvotes: 1

Views: 91

Answers (2)

Stewie Griffin
Stewie Griffin

Reputation: 14939

You can use inputname.

You must create a function for this to work, since inputname returns the name of the input variables to a function. First create an anonymous function that takes one input x, and returns the name of said variable. Then call this function with the variable you want to use:

variable_name = @(x) inputname(1);
str = sprintf('Spectral analysis - WELCH - %s', variable_name(X)); 

Note, you must change %d to %s, since you want a string, and not a digit.

Upvotes: 1

GameOfThrows
GameOfThrows

Reputation: 4510

Maybe I am understanding the question wrong, you want the Variable Name to appear instead of the values in the variable? So something like this?

 VariableName = 1:5;
 structure = whos
 VarName = structure(1).name 
 CompleteVarName = genvarname(repmat({VarName}, 1, 6), 'VarName');

 str=sprintf('Spectral analysis - WELCH - %s ', CompleteVarName{2:6});

 >> str

 str =

 Spectral analysis - WELCH - CompleteVarName1 Spectral analysis - WELCH - CompleteVarName2 Spectral analysis ...

Upvotes: 0

Related Questions