How do I get gif file function to work properly?

7 visualizaciones (últimos 30 días)
Umut Ayyildiz
Umut Ayyildiz el 24 de Mzo. de 2021
Comentada: DGM el 24 de Mzo. de 2021
Hello I am trying to get the gif from my following code by I keep running into an error. This is my first time dealing with it so I am not sure where my issue is.
clear all; close all
% Initial Conditions
theta_p= 0.01*pi/180; % initial theta position (rad)
%theta_v=0; % inital theta velocity (rad/s)
beta_p=90*pi/180-theta_p;
beta_v=0;
d_p=0.15; %inital position of collar A (m)
d_v=0; %inital velocity of collar A (m/s)
g=9.81; %gravity constant (m/s^2)
L=0.3; % length of bar (m)
mB=2; % mass of bar (kg)
mA=1; %mass of collar (kg)
dt=.0001;
I = (mB*L^2)/12;
Wa=mA*g;
Wb=mB*g;
x=(mB*L)/2;
z=(L/2);
for i = 1:50000
A = [-1 0 -mA 0;
1 0 -mB (x*sin(beta_p))
0 1 0 -x*cos(beta_p)
(sin(beta_p)*z) (-cos(beta_p)*z) 0 -I];
B = [0;
-x*(beta_v)^2*cos(beta_p);
Wb-x*(beta_v)^2*sin(beta_p);
0];
X=A\B;
beta_v = beta_v + X(4)*dt;
beta_p=beta_p + beta_v*dt;
d_v=d_v+X(3)*dt;
d_p=d_p+d_v*dt;
N = X(2)+Wa;
time(i)= i*dt;
dp(i)=d_p;
dv(i)=d_v;
beta(i)=beta_p;
omega(i)=beta_v;
Rx(i)=X(1);
Ry(i)=X(2);
d_a(i)=X(3);
beta_a(i)= X(4);
end
%% Animation Setup
close all; clc;
scale = 10;
% Collar A
xBl = scale*[-0.02 0.02 0.02 -0.02];
yBl = scale*[0.02 0.02 -0.02 -0.02];
% Pin
gam = linspace(0,2*pi,100);
xPin = scale*0.005.*cos(gam);
yPin = scale*0.005.*sin(gam);
% Swinging Bar:
xSBar = scale*[-0.02 0.25 0.25 -0.02];
ySBar = scale*[0.01 0.01 -0.01 -0.01];
% Frictionless Rod
xFBar = scale*[-0.2 0.2 0.2 -0.2];
yFBar = scale*[0.01 0.01 -0.01 -0.01];
%% Animation
% Initialize figure
h = figure;
filename = 'Test2.gif';
% Initialize some vectors
xBlP = xBl;
yBlP = yBl;
xPinP = xPin;
yPinP = yPin;
xSBarP = xSBar;
ySBarP = ySBar;
for i=1:length(time)
if mod(i,200) == 0
clf
% Update coordinates of shapes
% Rotation of bar
xSBarP = xSBar*cos(beta(i)) - ySBar*sin(beta(i));
ySBarP = xSBar*sin(beta(i)) + ySBar*cos(beta(i));
% Translate of bar
xSBarP = dp(i)+xSBarP;
% Translation of block
xBlP = dp(i)+xBl;
yBlP = yBl;
% Translation of pin
xPinP = dp(i)+xPin;
yPinP = yPin;
% Plot shapes
patch(xFBar, yFBar,'b');
patch(xBlP, yBlP, 'y');
patch(xSBarP, ySBarP, 'r');
patch(xPinP, yPinP, 'k');
axis('square'); axis([-2.5 2.5 -2.5 2.5]); axis('off');
% Process frame
frame = getframe(h);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if i == 1
imwrite(imind,cm,filename,'gif','Loopcount',inf);
else
imwrite(imind,cm,filename,'gif','WriteMode','append');
end
end
end
  4 comentarios
Walter Roberson
Walter Roberson el 24 de Mzo. de 2021
I had no problem with the getframe()
The code already has
h = figure;
and there should be no need to generate another figure.

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 24 de Mzo. de 2021
imwrite cannot handle different colormaps for each frame. You have to write in rgb mode. It will automatically reprocess the color data of the appended frames to match the colormap it deduced from the first (rgb) frame.
Yes, this is more limited than one might prefer, and does tend to imply that you should write a colorful frame first so colors on the remaining frames are not accidentally mapped through a restricted colormap.
  2 comentarios
Umut Ayyildiz
Umut Ayyildiz el 24 de Mzo. de 2021
I've doen another animation using the same format in my code above and it still worked. I'm still sort of confused as to what I need to do.
This is the code I am refering to where it worked:
clear all; close all
m = 0.5; %mass kg
dt = .04; % timestep
r(1) = 0.8; %r position m
r_vel(1)=0; %r velocity m/s
theta(1) = .7854; %theta position rad
theta_vel(1)=0; %theta velocity rad/s
theta_acc = 10; %theta constant acceleration rad/s^2
g=9.81; %gravity constant m/s^2
for ii = 1:200
A = [sin(theta(ii))/m -(sin(theta(ii))+2*dt*theta_vel(ii)*cos(theta(ii))+2*dt^2*theta_acc*cos(theta(ii)))
cos(theta(ii))/m -(-cos(theta(ii))+2*dt*theta_vel(ii)*sin(theta(ii))+2*dt^2*theta_acc*sin(theta(ii)))];
B=[2*r_vel(ii)*theta_vel(ii)*cos(theta(ii))+2*r_vel(ii)*dt*theta_acc*cos(theta(ii))+r(ii)*theta_acc*cos(theta(ii))-r(ii)*(theta_vel(ii)+theta_acc*dt^2)*sin(theta(ii))
2*r_vel(ii)*theta_vel(ii)*sin(theta(ii))+2*r_vel(ii)*dt*theta_acc*sin(theta(ii))+r(ii)*theta_acc*sin(theta(ii))-r(ii)*(theta_vel(ii)+theta_acc*dt^2)*cos(theta(ii))+g];
X=inv(A)*B;
r_acc(ii+1)=X(2); %r acceleration
F(ii+1)=X(1); %force
T(ii+1)=dt*ii; %time
theta_vel(ii+1)=theta_vel(ii)+theta_acc*dt; %theta velocity
theta(ii+1)=theta(ii)+theta_vel(ii+1)*dt; %theta position
r_vel(ii+1)=r_vel(ii)+r_acc(ii+1)*dt; %r velocity
r(ii+1)=r(ii)+r_vel(ii+1)*dt; %r position
end
%%
%%% Animation
%%% Block
Xb = [-25 -22 -22 -25];
Yb = [-1 -1 1 1];
%%%Holder Outside
Xout = [-39 3 3 -39];
Yout = [-1.75 -1.75 1.75 1.75];
%%%Holder Inside
Xin = [-38 -10 -10 -38];
Yin = [-1 -1 1 1];
%%% Base
alpha = linspace(0,2*pi,20);
X0 = 1*cos(alpha);
Y0 = 1*sin(alpha);
%%
close all
%%%% Animation Set up
h = figure;
filename = 'Project1Animation.gif';
for ii = 1:length(T)
clf
for jj = 1:4
Xbp(jj)=(Xb(jj)+r(ii))*cos(theta(ii))-(Yb(jj))*sin(theta(ii));
Ybp(jj)=(Xb(jj)+r(ii))*sin(theta(ii))+(Yb(jj))*cos(theta(ii));
Xoutp(jj)=(Xout(jj))*cos(theta(ii))-(Yout(jj))*sin(theta(ii));
Youtp(jj)=(Xout(jj))*sin(theta(ii))+(Yout(jj))*cos(theta(ii));
Xinp(jj)=(Xin(jj))*cos(theta(ii))-(Yin(jj))*sin(theta(ii));
Yinp(jj)=(Xin(jj))*sin(theta(ii))+(Yin(jj))*cos(theta(ii));
end
patch(Xoutp,Youtp,'b'); hold on
patch(X0,Y0,'g');
patch(Xinp,Yinp,'w');
patch(Xbp,Ybp,'r');
axis([-45 45 -45 45]);
axis('square');
axis('off')
%%%% Animation to GIF
frame = getframe(h);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
% Write to the GIF File
if ii == 1
imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,filename,'gif','WriteMode','append');
end
end
DGM
DGM el 24 de Mzo. de 2021
Unless something changed, imwrite can write multiframe gifs with different colormaps per frame. The problem is with imread. Imread cannot properly read multiframe gifs with unique local color tables.
Historically, there were workarounds, but after R2018b, the process of using imread on a multiframe gif will destructively remap all frames to use the calculated color table for frame 1.

Iniciar sesión para comentar.

Categorías

Más información sobre Animation en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by