How to "extract" a variable name to a string ?
179 views (last 30 days)
Show older comments
Hi
I need to make a lot of plot to debug my code.
For this purpose, I have a function that plots the vector I give her as input.
I would like to be able to get the name of the variable I am plotting, extract it in a string and use it to title my plot.
How can I do this in Matlab ?
Thank you for your help.
Illustrative, oversimplified example :
function plottingFct(quantityToPlot)
% Simplified example of what I would like to do
figure
plot(quantityToPlot)
NameOfVariableIPlot=ExtractNameOfVariable(quantityToPlot); % How can I do this ?
title(NameOfVariableIPlot);
end
0 Comments
Answers (5)
Alexey Budkov
on 14 Mar 2022
Try the following:
myvar = 1;
get_myvar_name = @(x) inputname(1);
get_myvar_name(myvar)
0 Comments
Walter Roberson
on 1 Mar 2019
NameOfVariableIPlot = inputname(1);
Note that this will be empty if the input was an expression.
0 Comments
Image Analyst
on 1 Mar 2019
Try this: inputname()
Example:
function myfun2(a,b,c)
for m = 1:nargin
disp(['Calling variable ' num2str(m) ' is ''' inputname(m) '''.'])
end
0 Comments
Star Strider
on 1 Mar 2019
function plottingFct(quantityToPlot)
% Simplified example of what I would like to do
figure
plot(quantityToPlot)
NameOfVariableIPlot=inputname(1); % How can I do this ?
title(NameOfVariableIPlot);
end
Qwerty = rand(1, 10);
plottingFct(Qwerty)
0 Comments
SIVAKUMAR KARURNKARAN
on 26 Dec 2022
Moved: Image Analyst
on 26 Dec 2022
just use following commend
strjoin(who)
0 Comments
See Also
Categories
Find more on Other Formats in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!