using a matlab string as a valid variable name???
89 views (last 30 days)
Show older comments
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 Comments
Stephen23
on 29 Sep 2017
Edited: Stephen23
on 30 Sep 2017
@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
on 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)
Answers (3)
Walter Roberson
on 3 Jan 2013
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 Comments
Rory Staunton
on 24 Apr 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
on 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
on 3 Jan 2013
You can use dynamic structure names but I've never found them to be necessary: http://www.mathworks.com/matlabcentral/answers/?term=dynamic+structure
0 Comments
See Also
Categories
Find more on Structures in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!