simple animation of moving lines
14 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello,
I have data calculated from a 4 bar linkage design, such as x,y for each joint. How can I make a simple movie animating the motion of the mechanism? I have tried for quite a while to make it work using getframe and movie. This is the code from my last attempt:
if true
figure
for j = 1:1:steps+1;
x12(j,1) = 0;
x12(j,2) = r12*cos(theta1(j));
y12(j,1) = 0;
y12(j,2) = r12*sin(theta1(j));
plot(x12(j), y12(j), '-')
MM(j) = getframe;
cla
end
movie(MM)
end
This is just for a simple line rotating about the origin. I apologize I am new to Matlab plotting/animations and can't seem to get it right. I have also tried using the line command as it seems it would be easy to make the simple lines of the 4 bar linkage. Can you tell me how to do this right?
Thanks
2 Kommentare
Jan
am 6 Mär. 2016
When I try to run your code, Matlab's mentions missing variables: steps, r12, theta. Please explain the problems you have with this code. What is your actual question?
Antworten (2)
Geoff Hayes
am 7 Mär. 2016
Stephen - part of the problem may be with the call to plot
plot(x12(j), y12(j), '-')
x12 and y12 are two dimensional arrays, and yet we are only trying to plot the jth element from each. I wonder if this should be instead
plot(x12(j,:), y12(j,:), '-')
so that we plot the origin and end coordinates for the (animating) line. Try the following code
figure;
axis([-5 5 -5 5])
hold;
hPlot = plot(NaN,NaN,'-');
theta1 = zeros(steps+1,1);
x12 = zeros(steps+1,2);
y12 = zeros(steps+1,2);
MM(steps+1) = struct('cdata',[],'colormap',[]);
for k = 1:1:steps+1;
theta1(k) = 2*pi/steps*(k-1);
x12(k,1) = 0;
x12(k,2) = r12*cos(theta1(k));
y12(k,1) = 0;
y12(k,2) = r12*sin(theta1(k));
set(hPlot,'XData',x12(k,:),'YData',y12(k,:));
MM(k) = getframe;
end
In the above, the limits for the axes are set (fixed) so that we can better observe the animating line. All arrays, including MM, are pre-sized so that we don't re-size the array on each iteration of the loop. The "dummy" plot
hPlot = plot(NaN,NaN,'-');
gives us a valid graphics object handle to for a plot that we can re-use on each iteration of the for loop. This is to avoid creating unnecessary graphics objects. We then update this graphic with
set(hPlot,'XData',x12(k,:),'YData',y12(k,:));
Note the indexing variable k. MATLAB uses i and j to represent the imaginary number, so it is good practice to avoid using variables with these names.
When I run the above, I observe the animating line and the movie plays as expected.
0 Kommentare
lasindu madushan
am 12 Aug. 2019
I want to create a a line which is moving in y = 0 line. help???
0 Kommentare
Siehe auch
Kategorien
Mehr zu Animation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!