How to "extract" a variable name to a string ?

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

답변 (5개)

Alexey Budkov
Alexey Budkov 2022년 3월 14일

2 개 추천

Try the following:
myvar = 1;
get_myvar_name = @(x) inputname(1);
get_myvar_name(myvar)
Walter Roberson
Walter Roberson 2019년 3월 1일

0 개 추천

NameOfVariableIPlot = inputname(1);
Note that this will be empty if the input was an expression.
Image Analyst
Image Analyst 2019년 3월 1일

0 개 추천

Try this: inputname()
Example:
function myfun2(a,b,c)
for m = 1:nargin
disp(['Calling variable ' num2str(m) ' is ''' inputname(m) '''.'])
end
Star Strider
Star Strider 2019년 3월 1일

0 개 추천

Use the inputname (link) function:
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)
SIVAKUMAR KARURNKARAN
SIVAKUMAR KARURNKARAN 2022년 12월 26일
이동: Image Analyst 2022년 12월 26일

0 개 추천

just use following commend
strjoin(who)

카테고리

도움말 센터File Exchange에서 Argument Definitions에 대해 자세히 알아보기

제품

질문:

2019년 3월 1일

이동:

2022년 12월 26일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by