Reputation: 85
I am having trouble with updating subplots. I've boiled my problem down to the following example:
win = figure(1);
win.sub1 = subplot(2, 2, 1);
win.sub2 = subplot(2, 2, 2);
win.sub3 = subplot(2, 2, 3);
win.sub4 = subplot(2, 2, 4);
x = 1:1:10;
plot(win.sub2, x, x); %graphs the line y = x in the second subplot, good.
hold on;
plot(win.sub2, x, -x) %ought to plot line y = -x over line y = x, but does not.
When executing the second plot, the first plot disappears despite the hold on. The only thing that seems to make this work is if I use axes(win.sub2), but I'm trying to avoid that because it really slows down my program (plotting 4 subgraphs on one figure, each with about 2 overlaid plots, to create a 1000+ frame movie). I appreciate any assistance. Thanks
Upvotes: 3
Views: 10191
Reputation: 112699
The problem seems to be that hold on
does not affect the right axis. You can solve it using set(...,'Nextplot','add')
on the intended axis. To do it sumultaneaously on all axes, it's much easier if win
is an array instead of a struct. And by the way, line 1 is useless (win
is overwritten).
So, the code would be:
win(1) = subplot(2, 2, 1);
win(2) = subplot(2, 2, 2);
win(3) = subplot(2, 2, 3);
win(4) = subplot(2, 2, 4);
set(win,'Nextplot','add') %// set this for all axes in variable win
x = 1:1:10;
plot(win(2), x, x); %graphs the line y = x in the second subplot, good.
plot(win(2), x, -x) %ought to plot line y = -x over line y = x, but does not.
Upvotes: 2
Reputation: 18488
I am a bit puzzled why your example does not work as expected, but changing hold on;
to hold(win.sub2, 'on');
seems to produce the desired result.
Note: when executing your example code, matlab gives me a warning, probably because the second line overwrites win
as defined in the first line.
Upvotes: 2