inputname with numeric inputs
2 views (last 30 days)
Show older comments
I'm writing a function in which I'd like to log user inputs before evaluating any numeric expressions. The inputname function is helpful when inputs are variables from the workspace, but I can't figure out how to log an input expression like 1:2:100. Here's an example:
function inputstrings = myfunction(varargin)
for k = 1:length(varargin)
inputstrings{k} = inputname(k);
end
Evaluating the above for three inputs gives:
myfunction(1:2:100,X,magic(99))
ans =
'' 'X' ''
I would like:
myfunction(1:2:100,X,magic(99))
ans =
'1:2:100' 'X' 'magic(99)'
Any ideas how to do this?
1 Comment
Sean de Wolski
on 9 Dec 2014
Why?
Why not just replace '' with MATLAB Expression (like imtool does for example)
imtool(magic(99))
x = magic(99);
imtool(x)
Answers (2)
Sean de Wolski
on 9 Dec 2014
Edited: Sean de Wolski
on 9 Dec 2014
There are ways to do it (sometimes), for example
However, I still see no reason for it.
4 Comments
Sean de Wolski
on 10 Dec 2014
Edited: Sean de Wolski
on 10 Dec 2014
Chad, in my mind this is not the purpose of a legend. This is the purpose of the plot itself. The plot is supposed to show me the data in a way that I can interpret it. The legend is supposed to be a label for the data, not the data themselves, like: "x", "MATLAB expression", "MxN double", "User defined input".
Jan
on 10 Dec 2014
This kind of meta-programming is prone to bugs. There are several pitfalls you cannot avoid:
myfunction(exit)
Here the side-effect of the exit command cannot be ignored, because Matlab shuts down.
myfunction(magic(3));
magic = 1:5;
myfunction(magic(3));
Now the symbol "magic" changes its meaning from a function to a vector. Then parsing the string of the calling line will not help to identify the meaning.
If you really want meaningful names for a legend, the most reliable and direct way is to define the data together with a meaningful name, e.g. in a struct:
data.value = 1:2:1000;
data.title = '1:2:1000 degrees';
This will not solve the strange exit() example, but for non-pathological data it is clean and clear.
See Also
Categories
Find more on Line Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!