Borrar filtros
Borrar filtros

Updating value with conditional for loop

1 visualización (últimos 30 días)
Jade T
Jade T el 2 de En. de 2023
Comentada: Jade T el 3 de En. de 2023
I am struggling with being able to come up with the right sequencing of codes for creating a loop that updates values that are conditional upon the user/trial. This is what I have so far. I want the balance to display as formatted text (nested loop)and then that same "balance" needs to be stored for another ongoing loop. Ultimately, do I need this balance loop here or in my trial loop that subsumes this?
Balance=0;
for k=1:length(ntrials)
if pchoice == 1
if card_prob == 1||2|
Balance=(Balance+lossamt)
elseif (card_prob == 3)||(card_prob ==4)||card_prob ==5|
Balance=(Balance+gainamt)
if pchoice == 2
Balance= (Balance+0)
break;
end
end
% The above loop is for the DrawFormattedText below, however balance is continuously updating 29 times(loop)
% so it is also being used on every trial screen.
Screen('TextSize',win, 40);
if value_diff == 1
card_prob = randsrc(1,1,[1,2;0.2,0.8])% when key is pressed
if card_prob == 1
DrawFormattedText(win, 'Sorry, you did not win','center', 'center', [0 0 0]);
WaitSecs(2)
% DrawFormattedText(win, balance,'center', 'center', [0 0 0]);
elseif card_prob == 2
DrawFormattedText(win, 'You Won','center', 'center', [0 0 0]);
WaitSecs(2)
DrawFormattedText(win, balance,'center', 'center', [0 0 0]);
end
end
Screen('Flip', win)
  2 comentarios
Voss
Voss el 3 de En. de 2023
You've got some syntax errors due to what look like extra "|" characters:
if card_prob == 1||2|
% ^ here
Balance=(Balance+lossamt)
elseif (card_prob == 3)||(card_prob ==4)||card_prob ==5|
% ^ here
Jade T
Jade T el 3 de En. de 2023
Ok, I didnt know that would be a problem, thanks for pointing this out!

Iniciar sesión para comentar.

Respuesta aceptada

Sulaymon Eshkabilov
Sulaymon Eshkabilov el 2 de En. de 2023
There are a couple of errs in your code. Here is the corrected part of it.
Balance=0;
for k=1:length(ntrials)
if pchoice == 1
if card_prob == 1||2|
Balance=(Balance+lossamt)
elseif (card_prob == 3)||(card_prob ==4)||card_prob ==5|
Balance=(Balance+gainamt)
end
else %pchoice == 2
Balance= (Balance+0)
break;
end
end
...
  1 comentario
Jade T
Jade T el 3 de En. de 2023
ok, I didnt end the first conditional statement and was redundant with p==2. Thanks for helping!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by