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

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 Commenti

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 this works I will move it to an answer, but for now it is more of suggestion based on a quick look at what you have done.
I actually tried that as an option but the problem is, in the final GUI, there are two subplots that I am trying to update at the same time.
Each subplot contains two plots (let us call them supersub plots, just for simplicity shall we?) viz. spring and point.
When I try to assign handles to each of these supersub plots, the set command cannot access Zdata or position properties because these handles are part of a subplot which is in turn part of an axis handle.
The structure is like this:
The handle of spring1 is a child of axis1 and so is the handle of point1. If I had only the spring, the get (handle, 'Children') command works perfectly and updates the child handle. Since each axis has two children, this command does not work anymore since it does not know which Zdata to update, the spring or the point.
I have used a common plot and subplot because that's the only way Matlab can run both animations simultaneously without having to resort to timers.
The link I have mentioned in my question suggests a cell array and it seems to have worked for someone. But I don't know how to get it to work for me.
Maybe something else?
I'm still not sure I understand the problem. 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.
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!
You are welcome. I have put my two comments into an answer.

Accedi per commentare.

 Risposta accettata

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.

Più risposte (1)

1 Commento

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.

Accedi per commentare.

Categorie

Scopri di più su Graphics Performance in Centro assistenza e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by