フィルターのクリア

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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by