Updating value with conditional for loop
조회 수: 1 (최근 30일)
이전 댓글 표시
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
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
채택된 답변
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
...
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!