How to convert string to variable name ?

2,217 views (last 30 days)
I try to do like this
if true
Varname=matlab.lang.makeValidName(strcat('Indiv_Reg_',Month))
end
where Month is a input variable represent char '01,02,...,12'
My problem is I cannot refer variable such as 'Indiv_Reg_01' to get a data from this variable. For example, I cannot get data from Indiv_Reg_01{1,1} by using this code
if true
Varname{1,1}
end
How Can I fix this problem ?
thanks in advance.
  1 Comment
Stephen
Stephen on 13 Jan 2018
"My problem is I cannot refer variable such as 'Indiv_Reg_01' to get a data from this variable"
The problem is that you are hiding pesudo-indices in variable names. Turn them into real indices and your "problem" instantly goes away. Solving problems through better code design is always preferred to writing slow, buggy, hack code accessing variable names.

Sign in to comment.

Accepted Answer

Jan
Jan on 13 Jan 2018
Edited: Jan on 13 Jan 2018
This question is an evergreen. The solution is easy: Don't do this. It is a shot in your knee.
See:
Do not hide indices in the names of variables, but use indices instead: Let Indiv_Reg be a cell array and use the numerical value as index. This is much better than the indirection over the conversion to a string and hiding it in the names of dynamically created variables.
  2 Comments
Jan
Jan on 4 Jan 2019
@Vladislav Kargin: Exactly. Matlab is a tool and you can use it the wrong way. Many beginners in programming struggle with the problem of hiding indices in the names of variables, because they do not have enough experiences with the efficient using of arrays. This is equivalent to the question, how to use a hammer to push a screw into a piece of wood. Of course an expert will recommend a rotating screwdriver instead. This is the well known XY-problem. As usual, there is always another beginner, who does not understand the power of efficient programming methods.
If all Matlab experts and the original documentation give the same advice, it is time to rethink your impression, if this advice is "idiotic".
This is your first contribution in this forum and you start with a trial to be offensive. Good luck with your hammer.

Sign in to comment.

More Answers (3)

Luna
Luna on 4 Jan 2019
Edited: Luna on 4 Jan 2019
Hi all,
I agree with Jan & Stephen what they said about eval function. Don't use it. Instead use struct and arrays with dynamic naming.
For this example I have created a solution you can check:
Month = '01,02,03,04,05,06,07,08,09,10,11,12';
monthsArray = strsplit(Month,',');
for i = 1:numel(monthsArray)
Varnames{i} = matlab.lang.makeValidName(strcat('Indiv_Reg_',monthsArray{i}));
myStruct.(Varnames{i}) = randi(20,1,1);
end
myStruct.(Varnames{1,1}) % should give you a value of a random number
myStruct.Indiv_Reg_01 % same result above
ps: if you are forced to name your variables use this. Otherwise use directly indices of your Nx12 array.
  4 Comments
Luna
Luna on 1 Jun 2020
Believe me you will put much more effort to the solution you are asking for in your way rather than "just redoing all scripts you got".
You will spend time just for once redoing your code with indexing and implementing arrays. After that you will be free. Think that when you want to change a single variable name, that will force you to "redo all your scripts" again.
I only use eval if I'm running Simulink models with from Workspace blocks.
Don't use it if you really don't know what eval is used for. Here is some informations about justifiable usages of eval. These advices are from Matlab experts, at least they know some programming better than most.

Sign in to comment.



Gabor
Gabor on 18 Feb 2021
Edited: Gabor on 18 Feb 2021
Your answer does not work if you only have hammer and a screw available at hand (curvfitting tool does not accept structs as variable for eg.). In that case its perfect to use the hammer to pop that screw in fast :D
I found this:
assignin('base',var_name, value)
  1 Comment
Walter Roberson
Walter Roberson on 18 Feb 2021
Curvefitting toolbox accepts function handles for the fit model; you can pass whatever you like to those handles.
functions such as fit() accept data that might have been referenced such as FileStruct.(Xvar) where Xvar is a scalar string object or character vector that indicates which field to look for.
cftool() accepts passing x, y, z in on the command line.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!