How to convert string to variable name ?
古いコメントを表示
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 件のコメント
Stephen23
2018 年 1 月 13 日
"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.
採用された回答
その他の回答 (3 件)
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 件のコメント
Steven Lord
2019 年 1 月 4 日
A struct is one possible solution. Depending on what data the poster wants to store associated with each name, another possible solution would be to use a table where you can give each row or each variable (column) its own name.
A = magic(3);
t = array2table(A, 'VariableNames', {'Huey', 'Dewey', 'Louie'})
To retrieve the third element in the second variable you can use several different approaches, two of which are:
nine1 = t{3, 2}
nine2 = t.Dewey(3)
Luna
2019 年 1 月 4 日
Thanks for additional information, Steven! :)
joe zhong
2020 年 5 月 21 日
In fact, this two solutions should be the "Accepted" answer.
We all know we should don't do something, but things just happened, and we need a real solution rather than "just redo all scripts you got".
Luna
2020 年 6 月 1 日
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.
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)
4 件のコメント
Walter Roberson
2021 年 2 月 18 日
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.
paul harder
2021 年 12 月 15 日
編集済み: paul harder
2021 年 12 月 15 日
Fun thread! I agree with everything that has been said about the faults in indexing variable names, but I also believe it's rarely but sometimes necessary to create variables in a loop. In my case, I needed to create a list of variables in a very specific format so that they can be exported out to a different piece of software. Here's how to initialize vairalbes in a loop using the method shown above. But as stated by others, this should not be used as common practice, especially if further analysis is to be done on the data within matlab.
for i=1:3
assignin('base',['data_' num2str(i)],0)
end
"In my case, I needed to create a list of variables in a very specific format so that they can be exported out to a different piece of software."
You write that you need "a very specific format" (by which I presume that you mean that the variable name needs to have a very specific format, as of course the variable format itself is an unrelated topic). The most commonly-used MATLAB function for exporting data which relies on the variable names is SAVE, but of course SAVE has the -STRUCT option which completely avoids the need for dynamically named variables, so you must be using some other export function. Which one?
In the interest of helping other users understand the need you have, would you be so kind as to provide some sample code, or a description of the file format, or something similar so that we can understand why your use-case has this particular "need" which cannot be fulfilled with simpler, more efficient code.
Govind Narayan Sahu
2022 年 6 月 17 日
編集済み: Govind Narayan Sahu
2022 年 6 月 17 日
This works perfectly fine for me. Thanks a lot.
data = rand(1,8);
vars = {'a', 'b', 'd', 'e', 'f', 'g', 'h', 'i'};
index_vars = 1:8;
for k = 1:length(vars)
assignin('base',vars{k}, data(1, index_vars(k)))
end
Anshuman Agrawal
2020 年 6 月 19 日
編集済み: Anshuman Agrawal
2020 年 6 月 19 日
0 投票
Hopefully, what you wanted to do was covered here.
カテゴリ
ヘルプ センター および File Exchange で Surrogate Optimization についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!