How to create variable names

1 回表示 (過去 30 日間)
Avishka Karunaratne
Avishka Karunaratne 2016 年 2 月 18 日
回答済み: Bob Thompson 2019 年 6 月 27 日
for i = 1:10
Si = Example(i,:)
end
So I have this line of code and I want answers S1, S2, S3, S4 etc, however I only get Si for every answer and if i replace Si with S(i) or S{i} the code doesn't work , so how can I fix this ?
  1 件のコメント
Stephen23
Stephen23 2016 年 2 月 18 日
編集済み: Stephen23 2019 年 6 月 27 日
Your code will be much simpler, more reliable, and much more efficient if you just use indexing into one variable instead of trying to create lots of separate variables.

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

回答 (1 件)

Bob Thompson
Bob Thompson 2019 年 6 月 27 日
I'm not sure why this got resurrected more than three years later, but I guess I might as well put an answer on it, for future searchers.
To the best of my knowledge, it is not possible to dynamically create variable names in Matlab. Ultimately this is a good thing though, because Stephen's comment is correct that the code will be better if variables are indexed into arrays, rather than large groups of individually named variables. Matrices, afterall, are what Matlab was created to work with.

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by