using a matlab string as a valid variable name???
Afficher commentaires plus anciens
Hi,
Is it possible to use a string as a variable name to which I can assign values???
Trying to explain the problem below in a better way: Let's assume we have a variable
X = 'omega'; % string
I want to create a new variable named "omega" (assume double type) using the string "omega". i.e the new variable omega should be able to hold numerical values (eg below).
omega = [2 3 4 5];
The main intent is to write a generic function with which I can assign values/writes to files etc. by simply passing the variable name as string during function calls
Thanks and Regards
5 commentaires
Stephen23
le 24 Avr 2017
Creating and accessing variable names dynamically makes your code slow, buggy, hard to debug, and hard to read. The MATLAB documentation specifically warns against accessing variable names dynamically:
And there are other good reasons why this should be avoided:
Jim Bosley
le 28 Sep 2017
Modifié(e) : Stephen23
le 29 Sep 2017
Stephen, You've made a strong point, here and elsewhere, about dynamic variable names. Let me ask you a question in a different way. Suppose I have a program that has hundreds of parameters (with names p1, p2, ... etc. for clarity). I want an easy way to test the program with different parameterizations. I'd like to be able to have a function with a call like this:
>testpgrm p27 3.4
testpgrm would do something like
function testprgm(par, val)
var = inputnames(1);
assignin('base',var,val);
mainprgrm
end
It strikes me that the whole point of my test program is specifically to use a dynamic variable name. Any way around this that's better? Alternative approaches? Thanks, Jim So, the question is how can I pass a parameter name and a value I'd like to assign to that parameter name, and have the program do the assignment?
Thanks,
Jim
@Jim: The problem is here already:
hundreds of parameters (with names p1, p2, ... etc.
It is a bad design to have such a large set of independent variables. If they are store in an array or cell array, the solution gets trivial:
parameter = {p1, p2, p3, ...}
testpgm(parameter{k}, 3.4) % Run the k-th test
Of course creating the cell {p1, p2, ...} is ugly, but not necessary, if you create the set of data as an array from the beginning.
@Jim Bosley: you are begging the question by assuming that you will have lots of variables in your workspace. It is unlikely that you sat and wrote out hundreds of lines of code defining each numbered variable independently, which means that those variables must be imported, input, or created somehow. By deciding to create/import/... all of those variables like that, you fixed how you would have to access them later. So your code is inefficient by design.
If the main program is a script then you should really just write a function, and pass the required parameters in some arrays (which could be numeric, cell, struct, table,...). This would make passing the values efficient, trivially easy, easy to debug, easy to check, etc, etc. In this way you can also avoid processing data in the base workspace.
"You've made a strong point, here and elsewhere, about dynamic variable names."
All I have done is read a lot of discussions, documentation, and blogs on this topic, and collected them together into one list (for anyone who wants to learn how to write efficient MATLAB code. Not everyone is!). My points are no stronger than what the documentation states, which is the dynamically accessing variables is slow, buggy, and complex. Instead of asking me for my opinion you can read the same MATLAB documentation that I read (you know, the documentation written by the people who wrote MATLAB), which explicitly recommends against what you are trying to do, and also recommends how to pass data between workspaces (hint: the recommended way to is to pass input/output arguments).
In any case, the real solution to your inefficient code design is to design your code better (as Jan Simon already stated). Note that it is possible to write complex, buggy, inefficient code in any language, not just in MATLAB.
Steven Lord
le 29 Sep 2017
To follow on Jan's comment, if the names of the parameters are important or useful information consider using a struct array, a containers.Map object, or an object of your own creation. This also saves potentially having to pass dozens or hundreds of inputs to a function (which almost inevitably involves a couple minutes debugging exactly which input argument you omitted that led to the cryptic error message you just received.)
param = struct('pi', pi, 'euler', exp(1), 'g', 9.8);
q = 2*param.pi;
In that case if you needed to modify a field in param using a char vector containing the name of that field, you can use dynamic field names.
s = 'pi';
q2 = 2*param.(s)
Réponses (3)
X = 'omega';
eval([X '= [2 3 4 5]']);
This will create a variable with name omega.
Walter Roberson
le 3 Jan 2013
0 votes
In your situation, if you are wanting to associate files with variables, why not pass the file name as the argument, or why not fopen() the file and pass the file identifier as the argument ?
4 commentaires
Ponnu Prasad
le 3 Jan 2013
Walter Roberson
le 3 Jan 2013
Have you looked at inputname() ?
Rory Staunton
le 24 Avr 2017
Not sure what the OP needed, but inputname() is what I was looking for! I never knew about it but I will surely be making use of it in the future. thanks!
Jan
le 28 Sep 2017
@Rory: Use it with care. Code, which is based on the name of a variable attracts bugs and an overly complicated programming style. If you want to store a name, use a struct like:
X.name = 'TheName';
X.value = 3.14;
This is more flexible and clear than using
TheName = 3.14
and inputname to identify the name of the variable.
Image Analyst
le 3 Jan 2013
0 votes
You can use dynamic structure names but I've never found them to be necessary: http://www.mathworks.com/matlabcentral/answers/?term=dynamic+structure
Catégories
En savoir plus sur Structures dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!