Group named variables in matrix

1 回表示 (過去 30 日間)
Abcdefghi Abcdef
Abcdefghi Abcdef 2021 年 3 月 19 日
コメント済み: Stephen23 2021 年 3 月 19 日
Hello,
I have five variables named ssc_A, ssc_B, ssc_C, ssc_D and ssc_E, and I would like to group them in a vector called SSC.
Is it possible to make a for loop that would work like this?:
%random values for ssc_A through ssc_E:
ssc_A = 1;
ssc_B = 2;
ssc_C = 3;
ssc_D = 4;
ssc_E = 5;
for i = "A":"E"
SSC = [SSC;"ssc_"i]
end
  1 件のコメント
Stephen23
Stephen23 2021 年 3 月 19 日
"I have five variables named ssc_A, ssc_B, ssc_C, ssc_D and ssc_E, and I would like to group them in a vector called SSC."
I can already see where this is heading... let me guess, you don't really have just five, but actually have many such variables?
"Is it possible to make a for loop that would work like this?:"
It is certainly possible, but those methods appeal only to users who want to force themselves into writing pointlessly slow, complex, obfuscated, buggy code that is hard to debug. Everyone else avoids that approach, e.g. by importing file data into one variable using neat, simple, trivial, and very efficient indexing, just like MATLAB was designed for:
So far you have not told use the most important information: how did you get all of those variables into the workspace?

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

回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 3 月 19 日
  2 件のコメント
Abcdefghi Abcdef
Abcdefghi Abcdef 2021 年 3 月 19 日
So I should do this manually? What if I have lots of variables? Is there an alternative?
Walter Roberson
Walter Roberson 2021 年 3 月 19 日
If you have lots of variables you should ensure that they are not generated or imported as individually named variables.
The alternatives are discussed in the link.

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

カテゴリ

Help Center および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by