MATLAB Answers

simple animation of moving lines

75 views (last 30 days)
Stephen Kramer
Stephen Kramer on 6 Mar 2016
Answered: lasindu madushan on 12 Aug 2019
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 Comments

Jan
Jan on 6 Mar 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?
Stephen Kramer
Stephen Kramer on 7 Mar 2016
This is the complete code. Thanks for responding to my request.
figure
r12 = 1;
steps = 200
for j = 1:1:steps+1;
theta1(j) = 2*pi/steps*(j-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)
I want to plot 3 to 4 2D lines from data points stored in variables. These (x,y) data points are the line endpoints. A set of data points are calculated for each point of a varying input value. When you draw lines between a set of data points you get a visualization of the 4-bar mechanism configuration. I'd like to plot the sets of lines successively in order to make an animation of the mechanism motion. The above code is for the input line which is a simple vector originating at the cs origin (0,0) with the tip rotating about the origin.
Thanks

Sign in to comment.

Answers (2)

Geoff Hayes
Geoff Hayes on 7 Mar 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 Comments

Sign in to comment.


lasindu madushan
lasindu madushan on 12 Aug 2019
I want to create a a line which is moving in y = 0 line. help???

  0 Comments

Sign in to comment.

Sign in to answer this question.