How to create variables from variable names stored in a array?

15 ビュー (過去 30 日間)
Atharva Bhomle
Atharva Bhomle 2023 年 6 月 5 日
編集済み: Stephen23 2023 年 6 月 5 日
attributes={'time', 'step', 'cuurent', 'voltage', 'chgAh', 'disAh'};
I have stored variable names in this array and i want to store data in them when using in loop. For eg.
if i call out attributes(2)== {some values} then i want this to create a variable named 'step' and load the given data in it. How can i do it?

採用された回答

kei hin
kei hin 2023 年 6 月 5 日
attributes={'time', 'step', 'cuurent', 'voltage', 'chgAh', 'disAh'};
prompt = 'input what you want: ';
input_str = input(prompt,'s');
exp_idx = '([0-9]*)';
[~,idx_str] = regexp(input_str,exp_idx,'tokens','match');
posi_start = strfind(input_str,'==');
value_str = input_str(posi_start+2:end);
idx = str2num(idx_str{1,1});
var_name = attributes{1,idx};
if isempty(str2num(value_str))
value = value_str;
else
value = str2num(value_str);
end
assignin('base',var_name,value);
  1 件のコメント
Stephen23
Stephen23 2023 年 6 月 5 日
編集済み: Stephen23 2023 年 6 月 5 日
Note that using ASSIGNIN to dynamically name variables has all of the disadvantages given here:

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by