How to change variable name in each loop iteration?

Say that I have a workspace containing vectors (or cells) with names 'test_1', 'test_b', and 'test_3'. I want to run a for loop (for j=1:3) which will in every iteration run over one of the three vectors in my workspace and perform a certain operation on it. For example:
for j=1:3
test_?(:,1) = 10;
end
This should acceses all elements in the first column of vector test_? and assign value of 10 to them. So my question is how to do this for varying end of the vector name (1, b, 3 in this particular case).
Thank you

回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2015 年 7 月 28 日
編集済み: Azzi Abdelmalek 2015 年 7 月 28 日

1 投票

But if you want to learn how to do it:
str={'1','b','2'}
for k=1:3
assignin('base',sprintf('test_%s', str{k}),10)
end

カテゴリ

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

質問済み:

2015 年 7 月 28 日

編集済み:

2015 年 7 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by