Get names of input variables - problem with inputname()
19 次查看(过去 30 天)
显示 更早的评论
I would like to use the variables passed to a function with the variable names that were used during the function call (with a variable number of possible input arguments). I realize that there exists the function inputname() to help me realize this:
c = myFun(varargin)
for i = 1:nargin
strVarName = inputname(i);
switch strVarName
case 'a'
a = varargin{i};
case 'b'
b = varargin{i};
otherwise
error(['Unknown input parameter: ' strVarName])
end
c = a + b;
end
This works fine for me, but the code looks a bit ugly, so I wanted to move the loop to a script that was be called at the beginning of the function. Initially this worked fine, but suddenly I kept getting the error 'Unknown input parameter: ' – without having made any changes to the code! If I check it in debug mode, I either get an empty string for for inputname for all inputs, or the entire Matlab application crashes upon the function call inputname() – and sometimes, it also works perfectly fine. I moved the code back from the script to the original function, it seems to work more stable now, but I wonder whether there is any specific reason for this strange behaviour?
I am aware that there exist other solutions to get the input variables with their original names. But I do not want to use a struct or a cell array in this particular function (and even with parseInput I only get a struct), so the only other solution that I found was to use parameter/value pairs, i.e. a function call of the form:
myFun(‘a’,a,’b’,b)
But I would prefer the original function call for usability, unless there are some fundamental issues with the inputname() function – so my question is: is the inputname() function so fundamentally unstable that it would be wiser to sacrifice some usability and use a less user-friendly solution like parameter/value-pairs?
And do you have any better solution for achieving the desired functionality? Given that the function is called very often throughout my program, any suggestion for optimizing execution time would be very welcome.
Thanks!
PS: I am aware that inputname() returns an empty string when the variable passed is an expression, but this should not be the problem here (especially considering that the same code also works sometimes).
回答(2 个)
Jan
2013-3-6
This is a really bad programming method. You store essential information in the name of a variable. But the name should only be a name and kept separate from the contained data. Imagine you want to rename one of the variables in a function, but this has severe side-effects in the subfunction using your method. And this is something programmers want to avoid under all circumstances, because it let the effeort needed for debugging grow exponentially with the number of variables, code lines and subfunction.
So my advice is clear: Do not do this. You cen get better troubles for less money.
Anyhow, Matlab should not crash. You have obviously found a severe bug, which has not been revealed by others, because they avoid the evil inputname consequently. When you are able to reproduce it, please send a description to the technical support.
0 个评论
Christoph
2013-3-6
编辑:Christoph
2013-3-6
2 个评论
Jan
2013-3-6
Please post comments as comments, because they are not answers.
Equivalent to Matlab's Handle Graphics system, you can add a field called "Type" to the structs:
% In the caller
Config.Type = 'Config';
Data.Type = 'Data';
% Inside the called function:
function c = myFun(varargin)
for k = 1:nargin
Arg = varargin{k};
switch Arg.Type
case 'Config'
Config = Arg;
case 'Data'
Data = Arg;
otherwise
error('Unknown input parameter: %s', Arg.Type);
end
You can move this to an extra function, which controls the modifications also:
function [Config, Data] = myParser(varargin)
Config = ... any defaults
Data = ... other defaults
% Now the code to identify the structs
...
% Finally the temporary changes:
...
You are very near to an object oriented approach then.
Go one step further: Why not storing all data in one struct? Then the fieldnames have the role of the former names of the variables, but in a direct and much saver and clearer way.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!