How would I create a loop to plot graphs

3 ビュー (過去 30 日間)
timetry2
timetry2 2019 年 10 月 3 日
コメント済み: Walter Roberson 2019 年 10 月 3 日
I have a code that looks like the following:
figure
y =Data.StrideTimeIntervals_15minTrial.PD(:,1);
subplot (3,4,1),plot (y, 'r')
title ('PD 1')
xlabel ('ISI #')
ylabel ('ISI (s)')
axis ([0,500,0.8,1.2])
disp ('PD 01 DONE')
plot mean(y, 'g', 'linewidth',3);
y =Data.StrideTimeIntervals_15minTrial.PD(:,2);
subplot (3,4,2),plot (y, 'r')
title ('PD 2')
xlabel ('ISI #')
ylabel ('ISI (s)')
axis ([0,500,0.8,1.2])
disp ('PD 02 DONE')
y =Data.StrideTimeIntervals_15minTrial.PD(:,3);
subplot (3,4,3),plot (y, 'r')
title ('PD 3')
xlabel ('ISI #')
ylabel ('ISI (s)')
axis ([0,500,0.8,1.2])
disp ('PD 03 DONE')
y =Data.StrideTimeIntervals_15minTrial.PD(:,4);
subplot (3,4,4),plot (y, 'r')
title ('PD 4')
xlabel ('ISI #')
ylabel ('ISI (s)')
axis ([0,500,0.8,1.2])
disp ('PD 04 DONE')
y =Data.StrideTimeIntervals_15minTrial.PD(:,5);
subplot (3,4,5),plot (y, 'r')
title ('PD 5')
xlabel ('ISI #')
ylabel ('ISI (s)')
axis ([0,500,0.8,1.2])
disp ('PD 05 DONE')
y =Data.StrideTimeIntervals_15minTrial.PD(:,6);
subplot (3,4,6),plot (y, 'r')
title ('PD 6')
xlabel ('ISI #')
ylabel ('ISI (s)')
axis ([0,500,0.8,1.2])
disp ('PD 06 DONE')
y =Data.StrideTimeIntervals_15minTrial.PD(:,7);
subplot (3,4,7),plot (y, 'r')
title ('PD 7')
xlabel ('ISI #')
ylabel ('ISI (s)')
axis ([0,500,0.8,1.2])
disp ('PD 07 DONE')
y =Data.StrideTimeIntervals_15minTrial.PD(:,8);
subplot (3,4,8),plot (y, 'r')
title ('PD 8')
xlabel ('ISI #')
ylabel ('ISI (s)')
axis ([0,500,0.8,1.2])
disp ('PD 08 DONE')
y =Data.StrideTimeIntervals_15minTrial.PD(:,9);
subplot (3,4,9),plot (y, 'r')
title ('PD 9')
xlabel ('ISI #')
ylabel ('ISI (s)')
axis ([0,500,0.8,1.2])
disp ('PD 09 DONE')
y =Data.StrideTimeIntervals_15minTrial.PD(:,10);
subplot (3,4,10),plot (y, 'r')
title ('PD 10')
xlabel ('ISI #')
ylabel ('ISI (s)')
axis ([0,500,0.8,1.2])
disp ('PD 10 DONE')
y =Data.StrideTimeIntervals_15minTrial.PD(:,11);
subplot (3,4,11),plot (y, 'r')
title ('PD 11')
xlabel ('ISI #')
ylabel ('ISI (s)')
axis ([0,500,0.8,1.2])
disp ('PD 11 DONE')
y =Data.StrideTimeIntervals_15minTrial.PD(:,12);
subplot (3,4,12),plot (y, 'r')
title ('PD 12')
xlabel ('ISI #')
ylabel ('ISI (s)')
axis ([0,500,0.8,1.2])
disp ('PD 12 DONE')
How would I use a loop to repeat the subplots after the first one.

採用された回答

Walter Roberson
Walter Roberson 2019 年 10 月 3 日
for col = 1 : 12
y =Data.StrideTimeIntervals_15minTrial.PD(:,col);
subplot(3,4,col);
plot(y, 'r');
title( sprintf('PD %d', col) );
xlabel('ISI #')
ylabel('ISI (s)')
axis([0,500,0.8,1.2])
fprint('PD %d DONE\n', col);
end
  2 件のコメント
timetry2
timetry2 2019 年 10 月 3 日
What would the full line of code look like? I tried to run the code provided above and it didn't give me the same plots.
Walter Roberson
Walter Roberson 2019 年 10 月 3 日
The full code would look like,
figure
for col = 1 : 12
y = Data.StrideTimeIntervals_15minTrial.PD(:,col);
subplot(3,4,col);
plot(y, 'r');
title( sprintf('PD %d', col) );
xlabel('ISI #')
ylabel('ISI (s)')
axis([0,500,0.8,1.2])
fprint('PD %02d DONE\n', col);
end
The only difference is now I put the leading 0 into the PD DONE message to replicate "PD 09 DONE" instead of the "PD 9 DONE" that my previous version would have generated.
What differences are you observing in the plots?
Have you considered plotting everything on the same plot using
plot(Data.StrideTimeIntervals_15minTrial.PD);
xlabel('ISI #')
ylabel('ISI (s)')
legend({'PD 01', 'PD02', 'PD03', 'PD04', 'PD05', 'PD06', 'PD07', 'PD08', 'PD09', 'PD10', 'PD11', 'PD12'})

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by