Repeating or rerunning a loop

4 ビュー (過去 30 日間)
Olivia Krieger
Olivia Krieger 2019 年 4 月 1 日
編集済み: Olivia Krieger 2019 年 4 月 2 日
Hello MATLAB community! I am hopeful that someone can help me solve this puzzle I've created for myself...
I am running an experiment using an eye tracker. When an eye movement is detected, I want to record the trial sequence number and then add those trials onto the end of the trial block. But once I'm through the loop I don't know how to restart the loop with a new sequence/new number of iterations.
This is an example of my loop (the real loop is quite long and complex)
seq = [3, 1, 2, 1, 3, 2]; % trial sequence
nTrials = length(seq);
addTrials = [];
for trial = 1:nTrials
option = seq(trial);
if option == 1
% do this
elseif option == 2
% do that
elseif option == 3
% do another thing
end
movement = 0;
% check for eye movements
if eye movement is detected % this is not real code
movement = movement + 1
end
% add trial with movement to new sequence vector
if movement > 0
addTrials = [addTrials option];
end
end
% Question: how can I get the loop to rerun using
% nTrials = length(addTrials) and option = addTrials(trial)
I am also open to other ways to solve this than I've currently started!
Thanks!
  2 件のコメント
Jos (10584)
Jos (10584) 2019 年 4 月 1 日
In this pseudo-code you can omit the whole movement variable. Did you take a look at my answer?
Olivia Krieger
Olivia Krieger 2019 年 4 月 2 日
Hi, I check for eye movements in various places within the trial loop, so I've just added (from your suggestion)
if movement > 0
seq(end + 1) = option;
end
Thank you!!

サインインしてコメントする。

採用された回答

Jos (10584)
Jos (10584) 2019 年 4 月 1 日
Use a while loop, which is more flexible than a for -loop. Something like this might work:
seq = [3, 1, 2, 1, 3, 2]; % trial sequence
k = 0 ;
while k < length(seq),
k = k + 1 ;
option = seq(k);
% check for eye movements
if eye movement is detected % this is not real code
% add trial with movement to the sequence vector
seq(end+1) = option ;
end
end
  1 件のコメント
Olivia Krieger
Olivia Krieger 2019 年 4 月 2 日
編集済み: Olivia Krieger 2019 年 4 月 2 日
Thank you!! This is very helpful and should work to resolve my issue, it worked in a little psuedocode I made. I will implement today in the actual experiment!

サインインしてコメントする。

その他の回答 (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