adding variable in the name of table for while loop

1 回表示 (過去 30 日間)
Sunghwan Kim
Sunghwan Kim 2022 年 6 月 29 日
コメント済み: Sunghwan Kim 2022 年 6 月 29 日
I need to do the following job
Alq_4_t.Properties.VariableNames{1} = 'mz';
...
Alq_36_t.Properties.VariableNames{1} = 'mz';
So, I made a loop like this.
"i=4;
while i<37
Alq_i_t.Properties.VariableNames{1} = 'mz';
i=i+1;
end "
I run this script and there was no error. However, the variable names were not changed at all. Can anyone let me know why? Thanks in advance.
  1 件のコメント
Stephen23
Stephen23 2022 年 6 月 29 日
編集済み: Stephen23 2022 年 6 月 29 日
"Can anyone let me know why?"
Because a literal "i" in the variable name is not automagically updated using the value of some variable that happens to be named "i". Imagine the chaos that would ensue if that were the case.
Forcing pseudo-indices into the variable names forces you into writing slow, complex, inefficient, obfsucated, buggy code when you try to access your data in a loop:
In contrast, when using actual indices your code will be simpler and much more efficient. You should use indexing.
You have not told us the most important information: how did you get all of those variables into the workspace? I doubt that you named them all by hand...

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

採用された回答

Keshav
Keshav 2022 年 6 月 29 日
編集済み: Keshav 2022 年 6 月 29 日
you can not use i as a variable inside Alq_i_t variable name. and instead of making 36 different variable you can make an array of size of 36 by using below code.
i=1;
while i<37
Alq_t{i}.Properties.VariableNames{1} = 'mz';
i=i+1;
end

その他の回答 (1 件)

KSSV
KSSV 2022 年 6 月 29 日
i=4;
while i<37
Alq_i_t.Properties.VariableNames{i} = 'mz';
i=i+1;
end

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by