Reputation: 38
Inside my code I currently have 2 while loops looking like this:
sequence = [15 35 50];
startStamp = GetSecs %GetSecs is a psychtoolbox function that returns the current time in seconds
while GetSecs - startStamp < sequence(1) || (GetSecs - startStamp >= sequence(2) && GetSecs - startStamp <= sequence(3))
cond1 %display stimulus 1
end
while GetSecs - startStamp >= sequence(1) && GetSecs - startStamp < sequence(2)
cond2 %display stimulus 2
end
Whenever the timer (GetSecs - startStamp) reaches one of the elements of sequence I want to go from one while loop into the other and then execute it until the next element of sequence is reached, switch loops and so forth...
The way I structured my conditional statements for entering the while loops is not very slim but it becomes exponentially worse as numel(sequence) increases.
Is there a way to do this more elegantly and to work with a variable length of seqeunce?
Upvotes: 1
Views: 113
Reputation: 414
sequence = [15 35 50];
startStamp = GetSecs %GetSecs is a psychtoolbox function that returns the current time in seconds
while (Getsecs - startStamp) < sequence(end)
for i=1:length(sequence)
if (Getsecs-startStamp) < sequence(i)
break
end
end
if mod(i,2) == 1
cond1
else
cond2
end
end
Upvotes: 0
Reputation: 195
For variable length of array sequence, you can use one big while loop for looping through elements of the sequence array, and place flag inside the loop that will tell you which condition to use.
Translated to code:
counter = 1; flag = true;
while(counter <= length(sequence))
while(GetSecs - startStamp < sequence(counter))
if flag
cond1;
else
cond2;
end
end
flag = ~flag;
counter = counter + 1 ;
end
Upvotes: 1