How do I continuously update data for two plots (plotted on the same axis using plot on)?

16 views (last 30 days)
I have written a code to
  • Draw a spring
  • Draw a point on the spring
  • Animate the spring (up and down)
  • Animate the point to move along with the spring
Now, the animation works till the point where the spring alone has to move. If I try to plot the point, the animation stops and the spring and the point appear stationary.
I have plotted the spring and the point together on the same subplot (of an axes handle in a GUI). How can I update the Zdata of both the plots in a loop?
time = 0:pi/50:25*pi; % Time array
radius = 2; % Radius of the coil spring
height = 10; % Height of the coil spring
x = sin(time) * radius; % Compute x and y
y = cos(time) * radius;
z = height/(2*pi) * time; %Compute z which will be used later to animate
axis off
ha1 = subplot(1,2,1,handles.axes1);
plot3(x,y,z,'Linewidth',2);
set(ha1,'Position',[.016 .044 .142 .274]);
hold on
plot3(1.275, 1.545, 121.1, 'diamond',...
'LineWidth',1,...
'MarkerSize',10,...
'MarkerEdgeColor','b',...
'MarkerFaceColor', [0.7 0.7 0.7]);
pause('on');
axis off ;
while 1
t=0; axis off;
for t= 0:1:1000
zaddf = sind(2.5*2*pi*t);
znewf = z + zaddf;
Zdataarray = {znewf , zaddf + 121.1} ;
hChildren1 = get(handles.axes1,'Children');
axis off
set(hChildren1,'ZData',Zdataarray);
drawnow;
pause(0.005);
end
end
When I run this code, the ZDataarray variable (concept suggested by this answer) becomes a cell with two elements - one a [1 X 1251] array and the other a single numerical value. I am not able to set the ZData of both the plots in a loop.
When I try with this code,
set(hChildren1,'ZData',znewf);
the spring animation works perfectly on its own, without the point on it.
How can I update both plots simultaneously, in a loop?
Thanks in advance.
  5 Comments
Harikrishna Devarajan
Harikrishna Devarajan on 9 Nov 2016
Adam, I think I finally get what you are saying.
I was under the impression that a subplot cannot be defined separately for an axes handle. I tried your method, and I am pleased to report that it works very well. I will run it with the rest of the code just to make sure there are no conflicts from the second subplot.
For the benefit of the community,
subplot(1,2,1,handles.axes1);
ha1 = plot3(x,y,z);
hold on
ha2 = plot3(a,b,c);
while 1
set(ha1, 'ZData', Newvalue1);
set(ha2, 'ZData', Newvalue2);
drawnow
end
Please change this to an answer so that it becomes an accepted answer for the community to refer easily.
Thanks Adam for the tip!

Sign in to comment.

Accepted Answer

Adam
Adam on 9 Nov 2016
Can you not just keep hold of the output arguments of your two plot3 command so you have the handles to both the graphical objects then update both of them in individual instructions in your loop?
I've never tried updating two handles in one set instruction using a cell array so I'm not even sure what it does. I would just go for
hLine1 = plot3(x,y,z,...)
hLine2 = plot3(1.275, 1.545,...)
while 1
...
set( hLine1, 'ZData', zaddf + 121.1 );
set( hLine2, 'ZData', znewf );
...
end
as a setup, at least initially to see if it works. I wouldn't name my handles that, I'd name them something more meaningful, but I'm not sure precisely what each represents.
If you always keep the handles to the graphics objects you plot - i.e. the output of plot3 or plot or whatever plotting instruction you use then you have the handles of the objects you want to update and you can just update them directly. I very rarely need to resort to things like getting Children from axes or using findall to get hold of a graphics object because I just keep their handles when I plot them so I can access them directly. It doesn't matter then what setup of axes you have, whether they are subplots or whether the graphics are all on the same axes or different axes.

More Answers (1)

KSSV
KSSV on 8 Nov 2016
  1 Comment
Harikrishna Devarajan
Harikrishna Devarajan on 9 Nov 2016
Hi!
Thanks for the suggestion.
In your code, you have a stationary point (spring hinge point), and a moving object (spring) on the same plot. For every loop iteration, you have to update only the position of the spring, right?
In my case, I have to update both components simultaneously. I am still going through the entire code that you have suggested, and maybe I will have a solution soon.
Thanks again.

Sign in to comment.

Categories

Find more on Graphics Performance 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!