How to plot graph using for/while loop?

2 views (last 30 days)
I have a code that looks like the following:
This code plots subplots for the following, however I am needing to use a while or for loop to make it that a code will graph all 12 graphs without manually inputting the same code as I already have. Thank you in advance.
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')

Accepted Answer

the cyclist
the cyclist on 3 Oct 2019
Edited: the cyclist on 3 Oct 2019
I saw this question after I saw your question about plotting a mean line, and actually suggested a for loop there. For your convenience, here is what I suggested:
figure
for ii = 1:12
y =Data.StrideTimeIntervals_15minTrial.PD(:,ii);
subplot (3,4,ii),plot (y, 'r')
title (sprintf('PD %d',ii))
xlabel ('ISI #')
ylabel ('ISI (s)')
axis ([0,500,0.8,1.2])
disp (sprintf('PD %02d',ii))
end
The only slightly tricky part is being aware of using the sprintf command to be able to convert the loop variable into a string.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!