find beginning and end of events in a loop

5 views (last 30 days)
GB92_R
GB92_R on 11 Jan 2023
Answered: Divyanshu on 21 Apr 2023
Hi,
i have the diameter left and right (DialL and DialR) and the variable TOBII_Stim (=Stimulus).
I am trying to extract the DialL and DialR that correspon to specific condtions presents in the variable TOBII_Stim. For example, i have to extract the diameter L and R that goes from the beginning of the condition "Croix grise 200" until the end of the condition "Croix grise 200".
to do so, i created two empy variables that i will fulfill with the index of my beginning and end condition:
index_trial=[];
index_trial_end=[];
for i=1:length(TOBII_Stim)
if TOBII_Stim(i) == "Croix grise 200.jpg"
index_trial(end+1) = i;
elseif TOBII_Stim(i) == "Croix grise 85.jpg"
index_trial(end+1) = i;
elseif TOBII_Stim(i) == "Croix grise 255.jpg"
index_trial(end+1) = i;
elseif TOBII_Stim(i) == "RichText4.rtf"
index_trial(end+1) = i;
elseif TOBII_Stim(i) == "RichText3.rtf"
index_trial(end+1) = i;
elseif TOBII_Stim(i) == "RichText2.rtf"
index_trial(end+1) = i;
elseif TOBII_Stim(i) == "RichText1.rtf"
index_trial(end+1) = i;
end
end
i tried several things to find the end but i have errors saying that values must be integers.
Could please somebody help me out with understanding how to find the END of each condition and fulfill the variable index_trial_end=[]; ?
thnak you in advance
  2 Comments
Matt J
Matt J on 11 Jan 2023
Please provide example input variables and the desired output.
GB92_R
GB92_R on 11 Jan 2023
Hi Matt J,
i preferred to copy paste my work space to show you on which variable i am working on. I opened only DialL and TOBII stim to swhow you how they look like.
the objective is to fullfill the empty variable in this way:
for i=1:(length(index_trial)-1)
Trial{i}.index=index_trial(i);
Trial{i}.stim=TOBII_Stim(index_trial(i));
Trial{i}.baseline=(DiaL_LPF(index_trial(i)-65:index_trial(i)-1)+DiaR_LPF(index_trial(i)-65:index_trial(i)-1))/2;
Trial{i}.TIMEbaseline=TimeTOBII(index_trial(i)-65:index_trial(i)-1);
Trial{i}.timecourse=(DiaL_LPF(index_trial(i):index_trial(i)+1500)+DiaR_LPF(index_trial(i):index_trial(i)+1500))/2;
Trial{i}.TIMEtimecourse=TimeTOBII(index_trial(i):index_trial(i)+1500);
end
i=length(index_trial);
Trial{i}.index=index_trial(i);
Trial{i}.stim=TOBII_Stim(index_trial(i));
Trial{i}.baseline=(DiaL_LPF(index_trial(i)-65:index_trial(i)-1)+DiaR_LPF(index_trial(i)-65:index_trial(i)-1))/2;
Trial{i}.TIMEbaseline=TimeTOBII(index_trial(i)-65:index_trial(i)-1);
Trial{i}.timecourse=(DiaL_LPF(index_trial(i):end)+DiaR_LPF(index_trial(i):end))/2;
Trial{i}.TIMEtimecourse=TimeTOBII(index_trial(i):end);
%% save trials
save(Path_File(1:end-5),'Trial');

Sign in to comment.

Answers (1)

Divyanshu
Divyanshu on 21 Apr 2023
The reason for the error is that ‘TOBII_Stim is a cellular array and you are trying to access the elements of the cellular array using parenthesis () instead the cellular array elements should be accessed using angular brackets {}.
Have a look at the below sample code for better understanding:
index_trial=[];
index_trial_end=[];
TOBII_Stim = {"Validation.jpg" "RichText4.rtf" "Croix grise 200.jpg" "Croix grise 500.jpg" "Validation.jpg"}';
for i=1:length(TOBII_Stim)
if TOBII_Stim{i} == "Croix grise 200.jpg"
index_trial(end+1) = i;
elseif TOBII_Stim{i} == "Croix grise 85.jpg"
index_trial(end+1) = i;
elseif TOBII_Stim{i} == "Croix grise 255.jpg"
index_trial(end+1) = i;
elseif TOBII_Stim{i} == "RichText4.rtf"
index_trial(end+1) = i;
elseif TOBII_Stim{i} == "RichText3.rtf"
index_trial(end+1) = i;
elseif TOBII_Stim{i} == "RichText2.rtf"
index_trial(end+1) = i;
elseif TOBII_Stim{i} == "RichText1.rtf"
index_trial(end+1) = i;
end
end
index_trial
Please refer the following documentation for further information:

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!