How can I put these subplots into a for loop?

조회 수: 6 (최근 30일)
Maria Y
Maria Y 2019년 2월 8일
댓글: Adam Danz 2019년 2월 8일
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));

채택된 답변

Adam Danz
Adam Danz 2019년 2월 8일
편집: Adam Danz 2019년 2월 8일
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
Maria Y
Maria Y 2019년 2월 8일
Sorry, had your corrected version open in a different tab. Looks perfect, thanks so much for your help!
Adam Danz
Adam Danz 2019년 2월 8일
Nice! Glad I could help.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Subplots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by