필터 지우기
필터 지우기

Updating value with conditional for loop

조회 수: 1 (최근 30일)
Jade T
Jade T 2023년 1월 2일
댓글: Jade T 2023년 1월 3일
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
Voss
Voss 2023년 1월 3일
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 2023년 1월 3일
Ok, I didnt know that would be a problem, thanks for pointing this out!

댓글을 달려면 로그인하십시오.

채택된 답변

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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by