how to save variable with different name inside loop?

I have variable with name x with varying size of some 10000*2. 2 columns are fix, row value changing. I am getting that variable inside the for loop.
How should I autosave variable inside loop ? Autonaming for every loop condition will be useful.
Please reply. I want to use variable and call it in some other code.

1 件のコメント

Stephen23
Stephen23 2018 年 1 月 8 日
編集済み: Stephen23 2018 年 1 月 8 日
"Autonaming for every loop condition will be useful"
Only if your definition of "useful" is that it forces you to write slow, complex, buggy code that obfuscates the code intent and makes debugging much harder:
Just put your data into one array. Then this entire problem goes away. Solving problem through better code design is much preferred.

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

 採用された回答

Birdman
Birdman 2018 年 1 月 8 日

1 投票

Dynamically naming variables is not recommended. It is slow and buggy. Check the following link:
A better approach would be saving them in a structure by using a for loop. For instance:
for k = 1:10
A.(sprintf('RandomVariable_%d', k)) = rand;
end
The above code will automatically name the fields of struct. Your variables will be stored in a struct, which will be nice. Hope this helps.

3 件のコメント

Guillaume
Guillaume 2018 年 1 月 8 日
編集済み: Guillaume 2018 年 1 月 8 日
+1 for "Dynamically naming variables is not recommended". Autonaming for every loop condition will not be useful. It will quickly become unmanageable and there are much better ways.
-1 for using a structure. In my opinion, dynamically named structure fields are just as bad in this case. I would recommend using a cell array instead:
x = cell(numberofiterations, 1);
for loopiter = numberofiterations
...
x{loopiter} = some n x 2 matrix
end
And if the number of rows were fixed, I'd used a 3d matrix (I don't think it is the case here):
x = zeros(10000, 2, numberofiterations);
for loopiter = numberofiterations
...
x(:, :, loopiter) = some 10000 x 2 matrix
end
Birdman
Birdman 2018 年 1 月 8 日
I referred to the following link about dynamically naming structure fields.
Also, structures gives the flexibility to store several data types, therefore this situation makes them more attractive. Same thing applies for cell, of course but I believe it is just a matter of choice.
Jan
Jan 2018 年 1 月 8 日
A cell is a better solution, because in e.g. "A.RandomVariable_277" an index is hidden inside the field name. Extracting it requires a kind of sscanf or fprintf method with an expensive conversion between a number and a string. With using a cell, the index can be used directly.
Hiding a number in a name is an indirect method and therefore less clear. Structs are much clearer than cells, if the data are related to a name and not to a numerical index. Therefore I agree with Guillaume.

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

その他の回答 (0 件)

カテゴリ

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

質問済み:

2018 年 1 月 8 日

コメント済み:

Jan
2018 年 1 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by