'hold on' not working for Multi-position subplot in matlab

I am animating some subplots in MATLAB (R2015a). However, when I attempted to position a subplot to take up multiple positions the hold on command no longer works.

Here is a simplified form of my problem:

clear
N = 20;
Analysis(:,1) = linspace(1,N,N);
Analysis(:,2:5) = randi([1, 20],20,4);

    for n = 1:N;
        subplot(2,2,[1,2]);
        title('Particle counts at step number');
        plot(Analysis(n,1), Analysis(n,2), '.', 'Markersize', 8, 'color',      'red'), hold on;
        plot(Analysis(n,1), Analysis(n,3), '.', 'Markersize', 8, 'color', '[0,0.5,0]');
        legend({'Methane','Oxygen'},'FontSize',8,'FontWeight','bold', 'Location', 'northeastoutside');
        xlim([0,N]);
        ylim([0, 20]);


        subplot(2,2,[3,4]);
        title('Temperature(k) and Pressure');
        plot(Analysis(n,1), Analysis(n,4), '.', 'Markersize', 8, 'color', 'red'), hold on;
        plot(Analysis(n,1), Analysis(n,5), '.', 'Markersize', 8, 'color', 'blue');
        legend({'Temperature','Pressure'},'FontSize',8,'FontWeight','bold', 'Location', 'northeastoutside');
        xlim([0,N]);
        ylim([0, 20]);
        pause(0.1);
        drawnow;
    end

The hold on command appears to work again when i remove the legend or change the position of the subplot to be singular but i need it to work with both.

Answers


As I said above, this does appear to be a bug. One possible workaround is to modify the XData and YData properties of your line objects:

For example:

N = 20;
Analysis(:,1) = linspace(1,N,N);
Analysis(:,2:5) = randi([1, 20],20,4);

subplot(2,2,[1,2]);
title('Particle counts at step number');
hold on;
ph(1) = plot(Analysis(1,1), Analysis(1,2), '.', 'Markersize', 8, 'color',      'red');
ph(2) = plot(Analysis(1,1), Analysis(1,3), '.', 'Markersize', 8, 'color', '[0,0.5,0]');
hold off;
legend({'Methane','Oxygen'},'FontSize',8,'FontWeight','bold', 'Location', 'northeastoutside');
xlim([0, N]);
ylim([0, 20]);

subplot(2,2,[3,4]);
title('Temperature(k) and Pressure');
hold on;
ph(3) = plot(Analysis(1,1), Analysis(1,4), '.', 'Markersize', 8, 'color', 'red');
ph(4) = plot(Analysis(1,1), Analysis(1,5), '.', 'Markersize', 8, 'color', 'blue');
hold off;
legend({'Temperature','Pressure'},'FontSize',8,'FontWeight','bold', 'Location', 'northeastoutside');
xlim([0 ,N]);
ylim([0, 20]);

for n = 2:N;
    k = 2;
    for ii = 1:4
        ph(ii).XData = Analysis(1:n, 1);
        ph(ii).YData = Analysis(1:n, k);

        k = k + 1;
    end

    pause(0.1);
    drawnow;
end

I believe this gives you what you're looking for. Not the prettiest but it's functional.


Need Your Help

Why can't libcudart.so.4 be found when compiling the CUDA samples under Ubuntu?

c++ unix ubuntu cuda gpu-programming

I'm trying to get my Cuda SDK samples running, but I get the following error: