How do I get gif file function to work properly?
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Merve Buyukbas
le 24 Mar 2021
I think this link is useful: https://www.mathworks.com/matlabcentral/answers/526432-error-using-imwrite
Walter Roberson
le 24 Mar 2021
I had no problem with the getframe()
The code already has
h = figure;
and there should be no need to generate another figure.
Réponses (1)
Walter Roberson
le 24 Mar 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 commentaires
DGM
le 24 Mar 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.
Voir également
Catégories
En savoir plus sur Animation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!