How can I put these subplots into a for loop?

5 views (last 30 days)
Hello,
I've created these subplots which takes 300 seconds of data and chops it into 10 separate subplots of 30 second portions. I clearly did it manually here and I know there is a way to make it more efficient with a for loop, but i'm getting a bit confused on which values to choose to replace.
Thanks in advance for any and all help!
%% Load data
BR_file=dir('BRRAW*.mat');
load(BR_file.name);
%% Initialize
signal=[brraw.ecog, brraw.lfp]';
a=length(signal);
timex=[0:1/794:a/794];
timex(a+1)=[];
%% Subplots of 300 seconds of data chopped into 30 second portions
subplot(5,2,1)
time30=find(timex==30);
x=linspace(1,30);
hold on
plot(timex(1:time30),signal(1:time30));
subplot(5,2,2)
time60=find(timex==60);
x=linspace(30,60);
hold on
plot(timex(time30:time60),signal(time30:time60));
subplot(5,2,3)
time90=find(timex==90);
x=linspace(60,90);
hold on
plot(timex(time60:time90),signal(time60:time90));
subplot(5,2,4)
time120=find(timex==120);
x=linspace(90,120);
hold on
plot(timex(time90:time120),signal(time90:time120));
subplot(5,2,5)
time150=find(timex==150);
x=linspace(120,150);
hold on
plot(timex(time120:time150),signal(time120:time150));
subplot(5,2,6)
time180=find(timex==180);
x=linspace(150,180);
hold on
plot(timex(time150:time180),signal(time150:time180));
subplot(5,2,7)
time210=find(timex==210);
x=linspace(180,210);
hold on
plot(timex(time180:time210),signal(time180:time210));
subplot(5,2,8)
time240=find(timex==240);
x=linspace(210,240);
hold on
plot(timex(time210:time240),signal(time210:time240));
subplot(5,2,9)
time270=214381;
x=linspace(240,270);
hold on
plot(timex(time240:time270),signal(time240:time270));
set(gca,'Xlim',[240 270]);
subplot(5,2,10)
time300=find(timex==300);
x=linspace(270,300);
hold on
plot(timex(time270:time300),signal(time270:time300));

Accepted Answer

Adam Danz
Adam Danz on 8 Feb 2019
Edited: Adam Danz on 8 Feb 2019
Since we don't have access to your data, I've made some assumptions. Most importantly, I'm assuming 'timex' is monotonically increasing. If that's not correct, my solution won't work. Also, I did not include the x=linspace() line since you don't seem to be using that.
% define subplot layout
nrow = 5;
ncol = 2;
keyTimes = [0:30:300]; %you can probably come up with a better name for this variable
% Loop through each subplot
for i = 1: length(keyTimes)-1
subplot(nrow, ncol, i)
hold on %if needed
idx = timex >= keyTimes(i) & timex <=keyTimes(i+1);
plot(timex(idx), signal(idx))
end
  5 Comments
Maria Y
Maria Y on 8 Feb 2019
Sorry, had your corrected version open in a different tab. Looks perfect, thanks so much for your help!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!