1
votes

I'm trying to plot several subplots in a single figure. The total number of subplots is dependent on the maximal value in a matrix: maximal value of 'i'. I'd like to plot two variables in two subplots below each other for every value of i :slagtijd_start & slagfrequentie_start

So when the maximum value of i = 3, we have a subplot matrix of 2x3 axes, if i = 4 --> 2x4 axes etc.

Plotting on the right postions works, except that matlab erases the previous subplots (previous values of i). In this case i = 3 and the figure only displays the two latest subplots. I've tried different things with 'hold on' etc. But I can't figure it out. It's probably a simple trick.

I've posted my code and resulting figure below,

Thanks in advance,

Mochje

figure(6)
hold on

for i = 1:max(piektijden_start(:,2))
    startnummer = find(piektijden_start(:,2) == i); 
    slagtijd_start= diff(piektijden_start(startnummer)); 
    slagfrequentie_start= (60./slagtijd_start);
    subplot(2,i,i),plot(piektijden_start(startnummer(1:end-1),1),slagtijd_start)
    hold on
    xlabel('Tijd [s]')
    ylabel('Slagtijd [s]')
    title('Slagtijd')

    subplot(2,i,i+i),plot(piektijden_start(startnummer(1:end-1),1),slagfrequentie_start)
    hold on
    plot(piektijden_start(startnummer(1:end-1),1),slagfrequentie_start,'.r')
    xlabel('Tijd [s]')
    ylabel('Slagfrequentie [N/min]')
    title('Slagfrequentie')
end

enter image description here

1

1 Answers

2
votes

Use something like

figure(6)
hold on
max_i = max(piektijden_start(:,2));
for i = 1:max_i
    %// ...
    subplot(2, max_i, i)
    %// ...
    subplot(2, max_i, i+max_i)
    %// ...
end

The second argument to subplot is the number of subplot columns. The third is the addressed subplot, such that such that the first subplot is the first column of the first row, the second subplot is the second column of the first row, and so on