inputname with numeric inputs

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

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

Chad Greene
Chad Greene on 9 Dec 2014
Edited: Chad Greene on 9 Dec 2014
Hi Sean, thanks for your response. Regarding the reason, my function plots data that the user tells it to plot. I'd like to populate a reasonable legend based on the user's inputs. If the user inputs a variable called x, labeling that data in a legend is easy via inputname. If the user plots 1:2500, I'd rather not have the legend entry that would result from num2str(1:2500), and "Matlab Expression" is not very meaningful.
@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
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".
@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.

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

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

Tags

Asked:

on 9 Dec 2014

Commented:

on 10 Dec 2014

Community Treasure Hunt

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

Start Hunting!