How to save all values generated from a for statement in different variables

Hello,
I'm using a for statement to extract values from a matrix depending on i.
for i=1:intn % number of sources Ls= filename(i+1,5)
In this case, I need to save all generated values of Ls as Ls1, Ls2, Ls3, etc.. I tried Ls(i) but it didn't work. Does anyone know how to integrate i into the string name?
Any help would be highly appreciated!
Thanks,

3 件のコメント

Adam
Adam 2015 年 3 月 6 日
What do you mean by "I tried Ls(i) but it didn't work"?
That is clearly the best solution as it is exactly what an array is meant for so you would be best served by finding out why/what "doesn't work" and solving that.
Creating n variables with different names is not a solution to any problem.
Echidna's solution using structs with dynamic field names would also work, but is still more complicated than an array whose purpose is exactly this.
Belal Abboushi
Belal Abboushi 2015 年 3 月 6 日
When i used Ls(i), i got a variable Ls with two values in it (1x2 cell). Thanks,
Adam
Adam 2015 年 3 月 6 日
Unless intn is 2 it sounds like you used it incorrectly in that case, but you haven't posted your code so it is impossible to say much more.

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

回答 (1 件)

Ingrid
Ingrid 2015 年 3 月 6 日
you could try to use a structure that would look something like this:
for i =1:intn
currentName = ['Ls' num2str(i)]
results.(currentName) = filename(i+1,5);
end
than you can store the Ls1,Ls2,... as follows
save filename results -struct

カテゴリ

ヘルプ センター および File ExchangeDeep Learning Toolbox についてさらに検索

タグ

質問済み:

2015 年 3 月 6 日

コメント済み:

2015 年 3 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by