inputname with numeric inputs
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
0 votes
There are ways to do it (sometimes), for example
However, I still see no reason for it.
4 Comments
Chad Greene
on 9 Dec 2014
Edited: Chad Greene
on 9 Dec 2014
Jiro Doke
on 10 Dec 2014
@Chad,
Just some food for thought. How about using the "MxN double" notation for non-variable inputs, very much like what's shown in the Workspace Browser. Otherwise, if you manage to get the literal string of the input, someone could potentially call your function like this:
myfunction([1 4 3 2 6 5 3 5 3 57 4 4654 43 534 2 46 54 34 5435 6 43 543 754 543 234],X,y)
In that case, do you want your function to create a legend with such a long name? Or, even if it's not as contrived as this, it could be an output from a lengthy expression or a function call. For that reason, it might be good to think about an always-compact display.
Just my 2 cents.
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".
Chad Greene
on 10 Dec 2014
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.
1 Comment
Chad Greene
on 10 Dec 2014
Categories
Find more on Resizing and Reshaping Matrices 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!