How to create a single gobjects for different plots to avoid using clf
1 view (last 30 days)
Show older comments
I'm trying to use plot() several times without using clf because this causing my animation to blink. In the discussion in here [plot is blinking when clearing figure], a clever solution suggested by @Walter Roberson is to update data rather than creating a new object for plot every iteration. I've managed to create the following minimal working example as a start point for my problem. For each function (i.e. y1,y2), I need to create a new gobjects so that I can update their data rather than calling clf which causes terrible. The problem with the aforementioned approach is the fact that I need to create gobjects for each drawable thing. In my actual application, drawable things are unknown a priori. In the simuation, I have to add them depending on specific conditions. My question is how can I modify the below code in a clever way to use a shared gobjects and at the same time avoid using clf.
objarray = gobjects(1,2);
x=0:0.1:1;
y1=sin(x);
y2=cos(x);
for i=1:20
if ~ishghandle(objarray(1))
objarray(1) = plot(x,y1,'LineWidth',2);
else
x=0:.1:i;
y1=sin(x);
objarray(1).XData = x;
objarray(1).YData = y1;
end
if ~ishghandle(objarray(2))
if ~ishold
hold on
end
objarray(2) = plot(x,y2,'r','LineWidth',2);
else
x=0:.1:i;
y2=cos(x);
objarray(2).XData = x;
objarray(2).YData = y2;
end
axis([0 22 -2 2])
pause(.2)
end
The above code can be done with clf as follows which is not working for my case.
for i=1:20
clf
x=0:0.1:i;
y1=sin(x);
y2=cos(x);
plot(x,y1,'LineWidth',2);
hold on
plot(x,y2,'r','LineWidth',2);
hold off
axis([0 22 -2 2])
pause(.2)
end
0 Comments
Answers (1)
darova
on 9 Aug 2021
Try this way
x = 0:.2:10;
y1 = x + nan;
y2 = x + nan;
h1 = plot(x,y1,'r');
h2 = line(x,y2);
for i = 1:length(x)
y1(i) = sin(x(i));
y2(i) = cos(x(i));
set(h1,'ydata',y1);
set(h2,'ydata',y2);
pause(0.1)
end
0 Comments
See Also
Categories
Find more on Graphics Object Programming 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!