How to change variable name in each loop iteration?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Antworten (1)
Azzi Abdelmalek
am 28 Jul. 2015
Bearbeitet: Azzi Abdelmalek
am 28 Jul. 2015
This is a bad idea http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
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
0 Kommentare
Siehe auch
Kategorien
Mehr zu MATLAB Parallel Server finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!