Changing Variable Name during "While" loop

Hello,
I am writing with a "while" comment, and would like to change my variable name in each loop.
x = 1;
while x<=n
if Choice_Accuracy(x,1) == 1
Choice_RT_x = Choice_Input(x,1)
else Choice_RT_x = 0
end
x = x + 1:
end
Could someone tell me how I can Change the variable name "Choice_RT_x" to "Choice_RT_1", "Choice_RT_2", "..." with each loop? I know there are a lot of questions like these, but they are all with a "for" and I really don't understand it.
Thank you very much!

Risposte (3)

You can use the 'eval' function. Please see the following code:
x = 1;
while x<=n
if Choice_Accuracy(x,1) == 1
eval(['Choice_RT_' num2str(x) '= Choice_Input(x,1)']);
else
eval(['Choice_RT_' num2str(x) ' = 0']);
end
x = x + 1:
end

2 Commenti

This is not recommended!!
Thank you very much!

Accedi per commentare.

Luis Gomes Perini
Luis Gomes Perini il 15 Set 2015
Modificato: Luis Gomes Perini il 15 Set 2015
Hi,
it my help:
x = 1;
while x<=n
if Choice_Accuracy(x,1) == 1
eval(['Choice_RT_' num2str(x) '=Choice_Input(x,1);']);
else eval(['Choice_RT_' num2str(x) '=0;'])
end
x = x + 1;
end
Cheers

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Richiesto:

il 15 Set 2015

Modificato:

il 19 Giu 2019

Community Treasure Hunt

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

Start Hunting!

Translated by