how to use "getframe" commander for making animation video.

hello everybody
i want to make animation video.
but something matter. i think "F(i2) = getframe;" is problem.
but i don't know how to correct.
please help me.
thank you in advance.
d = fopen('displacement.dat','w');
v= fopen('velocity.dat','w');
a=fopen('acceleration.dat','w');
length1 = input('Enter length of link1:');
length2 = input('Enter length of link2:');
n1=input('Enter the number of image:');
t=0.0;
t_step =0.01;
t_end = 1.0;
local_r1 = [length1,0]';
local_r2 = [length2,0]';
while(t<=t_end)
theta_1=t;
theta_2=3*t;
r_d1=trans1(local_r1,theta_1);
r_d2=trans1(local_r2,theta_2);
r_td=addition(r_d1,r_d2);
r_v1=trans_velocity(theta_1,3,local_r1);
r_v2=trans_velocity(theta_2,1,local_r2);
r_tv=addition(r_v1,r_v2);
r_a1=trans_acceleration(3,0,r_d1);
r_a2=trans_acceleration(1,0,r_d2);
r_ta=addition(r_a1,r_a2);
fprintf(d,'%7.5f %15.6e %15.6e',t,r_td(1),r_td(2));
fprintf(d,'%15.6e %15.6e',r_d1(1),r_d1(2));
fprintf(d,'%15.6e %15.6e\n',r_d2(1),r_d2(2));
fprintf(v,'%15.6e %15.6e\n',r_tv(1),r_tv(2));
fprintf(a,'%15.6e %15.6e\n',r_ta(1),r_ta(2));
t=t+t_step;
end
fclose(d);
fclose(v);
fclose(a);
load displacement.dat
load velocity.dat
load acceleration.dat
time=displacement(:,1);
x_p=displacement(:,2); y_p=displacement(:,3);
rd1_xp=displacement(:,4); rd1_yp=displacement(:,5);
rd2_xp=displacement(:,6); rd2_yp=displacement(:,7);
x_p_dot=velocity(:,1); y_p_dot=velocity(:,2);
x_p_ddot=acceleration(:,1); y_p_ddot=acceleration(:,2);
subplot 331
plot(time,x_p,'r')
xlabel('Time(s)','Fontsize',14)
ylabel('x_p','Fontsize',14)
title('end-effector x','Fontsize',16)
legend('x_p&time','Location','SouthEast');
subplot 332
plot(time,y_p,'r')
xlabel('Time(s)','Fontsize',14)
ylabel('y_p','Fontsize',14)
title('end-effector y','Fontsize',16)
legend('y_p&time','Location','SouthEast');
subplot 333
plot(x_p,y_p,'r')
xlabel('x_p','Fontsize',14)
ylabel('y_p','Fontsize',14)
title('x-y relationship','Fontsize',16)
legend('x_p&y_p','Location','SouthEast');
subplot 334
plot(time,x_p_dot,'g')
xlabel('Time(s)','Fontsize',14)
ylabel('x_pdot','Fontsize',14)
title('velocity-x ','Fontsize',16)
legend('x_pdot&time','Location','SouthEast');
subplot 335
plot(time,y_p_dot,'g')
xlabel('Time(s)','Fontsize',14)
ylabel('y_p_dot','Fontsize',14)
title('velocity-y','Fontsize',16)
legend('y_pdot&time','Location','SouthEast');
subplot 336
plot(x_p_dot,y_p_dot,'g')
xlabel('x_p_dot','Fontsize',14)
ylabel('y_p_dot','Fontsize',14)
title('x-y velocity relationship','Fontsize',16)
legend('x_pdot&y_pdot','Location','SouthEast');
subplot 337
plot(time,x_p_ddot,'b')
xlabel('Time(s)','Fontsize',14)
ylabel('x_p_ddot','Fontsize',14)
title('acceleration-x','Fontsize',16)
legend('x_pddot&time','Location','SouthEast');
subplot 338
plot(time,y_p_ddot,'b')
xlabel('Time(s)','Fontsize',14)
ylabel('y_p_ddot','Fontsize',14)
title('acceleration-y','Fontsize',16)
legend('y_pddot&time','Location','SouthEast');
figure(2)
plot(x_p_ddot,y_p_ddot,'b')
xlabel('x_p_ddot)','Fontsize',14)
ylabel('y_p_ddot','Fontsize',14)
title('x-y acceleration relationship','Fontsize',16)
legend('x_pddot&y_pddot','Location','SouthEast')
k=VideoWriter('n2me2tion2.avi');
open(k);
for i2=1:length(time)
n2=fix(length(time)/n1);
figure(3)
hold on
axis equal
plot(x_p(i2),y_p(i2),'o-r','MarkerSize',3);
u1=line([0 rd1_xp(i2)],[0 rd1_yp(i2)],'Color','k','LineWidth',2);
u2=line([rd1_xp(i2) rd1_xp(i2)+rd2_xp(i2)],...
[rd1_yp(i2) rd1_yp(i2)+rd2_yp(i2)],'Color','b','LineWidth',2);
u3=line([x_p(i2) x_p(i2)+x_p_dot(i2)],...
[y_p(i2) y_p(i2)+y_p_dot(i2)],'Color','g','LineWidth',2);
if(i2==length(time))
break;
end
if(0==rem(i2,n2))
else
delete(u1)
delete(u2)
delete(u3)
end
F(i2) = getframe;
end
writeVideo(k,F);
close(k);
function r_prime= trans1(local_r1,theta_1)
A=[cos(theta_1),-sin(theta_1); sin(theta_1),cos(theta_1)];
r_prime=A*local_r1;
end
function r_dot=trans_velocity(theta,theta_dot,r_prime)
B=[-sin(theta),-cos(theta);cos(theta),-sin(theta)];
r_dot=theta_dot*B*r_prime;
end
function r_ddot=trans_acceleration(theta_dot,theta_ddot,r)
R=[0,-1;1,0];
r_ddot=theta_ddot*R*r-(theta_dot)^2*r;
end
function r=addition(r_prime1,r_prime2)
r=r_prime1 + r_prime2;
end

9 commentaires

Is there a reason you line() and then plot() and then delete the line before the plot is saved? I guess that could have an effect on setting axes limits, but xlim/ylim would be better for that purpose
because i want to trajectory of robot's motion
u=line([0 x_p(i2)],[0 y_p(i2)]); %x_p(i2), y_p(i2) is 100*1 matrix
That draws a single diagonal line on the axes
plot(x_p(i2),y_p(i2),'o-r','MarkerSize',3);
that draws a single point on the axes, with marker 'o' and marker size 3, with the color 'r'
delete(u)
that deletes the diagonal line before anyone could have seen the line being drawn and before anything took a record of the diagonal line.
Why are you drawing the diagonal line and then deleting it right after the plot() ?
oh i'm know my code is stupid.
but it doesn't matter.
i load my full code.
please help me.
i make correct figure(3) but no animation file.
what's the matter?
You seem to be using trans1() function, but I am not able to find a function with that name?
What error message are you getting?
Note: we would need all of your functions and all of your data files in order to test your function ourselves. Because we do not have that, you need to describe the problems more clearly.
% % oh so
oh sorry about my mistake.
this is my Error
Error: No video frames have been written to this file. The file may be invalid.
> VideoWriter/close (n.282 line)
VideoWriter/delete (no.217 line)
twoarm (no.116 line)
Variables of this type do not support point indexing.
Error: alternateGetframe
Error: getframe (67 line)
x = alternateGetframe(parentFig, offsetRect, scaledOffsetRect,
includeDecorations, h, offsetRectSpecified);
Error: twoarm (133 line) %twoarm is my main m-file
F(i2) = getframe;
You keep forgetting to upload the three .dat files, so how can we run your code???
i don't know what you say
it doesn't matter.
data file is in my code
open my three .dat file and write information and close and open in my code

Connectez-vous pour commenter.

 Réponse acceptée

Walter Roberson
Walter Roberson le 29 Nov 2020
Each time the XLim or YLim changes, MATLAB recalculates the exact size of the inner window. Sometimes, possibly due to round-off, it can end up with very slightly different widths, such as varying by +/- 1 pixel.
The key is to figure out the limits ahead of time, and enforce them.
I am not sure that changing them every iteration is strictly necessary after I pre-calculated them, but there were steps along the way in my debugging in which setting the limits each time was key to getting the frames the same size.
You will notice in the attached code that I parent every graphic operation -- tell it exactly which figure or axes the graphing pertains to. The code was not careful about which axes it was writing to, with the result that running the code twice could end up with quite a mess.

Plus de réponses (2)

It looks like your image might change size partway through. Try maximizing the figure in the loop after your last call to plot(), so it's always the same size for each frame (full screen).
g = gcf;
g.WindowState = 'maximized';

Catégories

En savoir plus sur Animation dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by