Clear Filters
Clear Filters

Updating value with conditional for loop

1 view (last 30 days)
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 Comments
Voss
Voss on 3 Jan 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 on 3 Jan 2023
Ok, I didnt know that would be a problem, thanks for pointing this out!

Sign in to comment.

Accepted Answer

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 2 Jan 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 Comment
Jade T
Jade T on 3 Jan 2023
ok, I didnt end the first conditional statement and was redundant with p==2. Thanks for helping!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!