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
Sean de Wolski
2014년 12월 9일
Why?
Why not just replace '' with MATLAB Expression (like imtool does for example)
imtool(magic(99))
x = magic(99);
imtool(x)
답변 (2개)
Sean de Wolski
2014년 12월 9일
편집: Sean de Wolski
2014년 12월 9일
0 개 추천
There are ways to do it (sometimes), for example
However, I still see no reason for it.
댓글 수: 4
Chad Greene
2014년 12월 9일
편집: Chad Greene
2014년 12월 9일
Jiro Doke
2014년 12월 10일
@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
2014년 12월 10일
편집: Sean de Wolski
2014년 12월 10일
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
2014년 12월 10일
Jan
2014년 12월 10일
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.
카테고리
도움말 센터 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!