inputname with numeric inputs

2 views (last 30 days)
Chad Greene
Chad Greene on 9 Dec 2014
Commented: Chad Greene on 10 Dec 2014
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
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)

Sign in to comment.

Answers (2)

Sean de Wolski
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
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
Chad Greene on 10 Dec 2014
@Jiro - You bring up a good point. I had not thought of that before. Nonetheless, I don't think long arrays will be an issue because this function is designed to make tinkering easy. That is, I want the user to be able to explore relationships between different arrays, and if the array is very long, the user has probably already saved it as a variable.
@Sean - I respectfully disagree; this seems like a fine use for a legend. Consider a function that plots any number of input arrays. To show a relationship between sine, cosine, and tangent the user might try:
x = 1:.1:4*pi;
myplot(sin(x),cos(x),tan(x))
A legend containing sin(x), cos(x), and tan(x) would be quite a bit more meaningful than three legend entries that say "1x116 array". This is certainly an issue I can work around, but I was hoping there'd be some simple solution. I figure if Jan, Jiro, and Sean weigh in without coming up with a simple solution, a simple solution does not exist. Thanks for your ideas though!

Sign in to comment.


Jan
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
Chad Greene on 10 Dec 2014
Hi Jan,
Indeed, meta-programming does seem dangerous. I may opt for a user prompt or something clunky that will get the job done more safely. Thanks for the tips.
Chad

Sign in to comment.

Categories

Find more on Line Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!