How would I create a loop to plot graphs

3 views (last 30 days)
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.

Accepted Answer

Walter Roberson
Walter Roberson on 3 Oct 2019
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 Comments
timetry2
timetry2 on 3 Oct 2019
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 on 3 Oct 2019
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'})

Sign in to comment.

More Answers (0)

Categories

Find more on Line Plots 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!