Error using imwrite()
11 views (last 30 days)
Show older comments
I'm getting an error in my function when using the imwrite() command. The error states:
Error using wgifc
Can only append to GIF89a format GIFs.
Error in writegif (line 306)
wgifc(mat, map, filename,writemode,disposalmethod,delaytime,...
Error in imwrite (line 559)
feval(fmt_s.write, data, map, filename, paramPairs{:});
Error in animateBird (line 127)
imwrite(imind,cm,filename,'gif','WriteMode','append');
Error in PlotRun3 (line 8)
animateBird('allPoints.mat', 1,'xy.gif')
Last week, there was no issue and I have not modified the code. Any help would be appreciated!
%% Import data
% imported .csv file
set(0,'DefaultLegendAutoUpdate','off');
load(dataMatrix);
%% Plot data on 3D plot
animPlot = figure(1);
hold on
% Right Wing
plot3(allPoints(18:300,1),... %x
allPoints(18:300,2),... %y
allPoints(18:300,3)); %z
% Left Wing
plot3(allPoints([1:138,142:164,171:191,196:222,225:267,285,288:293],4),... %x
allPoints([1:138,142:164,171:191,196:222,225:267,285,288:293],5),... %y
allPoints([1:138,142:164,171:191,196:222,225:267,285,288:293],6)); %z
% Beak
plot3(allPoints([66:263,278:292],7),... %x
allPoints([66:263,278:292],8),... %y
allPoints([66:263,278:292],9)); %z
% Left Tail
plot3(allPoints([51:65,72:256],10),... %x
allPoints([51:65,72:256],11),... %y
allPoints([51:65,72:256],12)); %z
% Right Tail
plot3(allPoints(47:259,13),... %x
allPoints(47:259,14),... %y
allPoints(47:259,15)); %z
grid on
% Select axis
if selection == 1
view(0,90) % XY
elseif selection == 2
view(0,0) % XZ
elseif selection == 3
view(90,0) % YZ
elseif selection == 4
view(-135,30)
end
legend('Right Wing','Left Wing','Beak','Left Tail','Right Tail')
xlabel('X')
ylabel('Y')
zlabel('Z')
rotate3d on;
%% Animate Bird
row = 1;
limit = length(allPoints);
while row <= limit
if isnan(allPoints(row,1)) ||isnan(allPoints(row,2)) ||isnan(allPoints(row,3)) ||isnan(allPoints(row,4)) ||isnan(allPoints(row,5)) ||isnan(allPoints(row,6)) ||isnan(allPoints(row,7)) ||isnan(allPoints(row,8)) ||isnan(allPoints(row,9)) ||isnan(allPoints(row,10)) ||isnan(allPoints(row,11)) ||isnan(allPoints(row,12))
row = row + 1;
else
% Get beak reference point
refBeak = [allPoints(row,7),... %x
allPoints(row,8),... %y
allPoints(row,9)]; %z
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Get corresponding right wing reference point
refRightWing = [allPoints(row,1),... %x
allPoints(row,2), ... %y
allPoints(row,3)]; %z
% Get vertical concatenation of beak and right wing
beakRightWing = [refBeak; refRightWing];
% Plot beak and right wing
brw = plot3(beakRightWing(:,1), beakRightWing(:,2), beakRightWing(:,3),'-ok');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Get corresponding left wing reference point
refLeftWing = [allPoints(row,4),... %x
allPoints(row,5), ... %y (There will be an error; it will still plot)
allPoints(row,6)]; %z Exception handling can be added later
% Get vertical concatenation of beak and right wing
beakLeftWing = [refBeak; refLeftWing];
% Plot beak and right wing
blw = plot3(beakLeftWing(:,1), beakLeftWing(:,2), beakLeftWing(:,3),'-ok');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Get corresponding right tail reference point
refRightTail = [allPoints(row,13),... %x
allPoints(row,14), ... %y
allPoints(row,15)]; %z
% Get vertical concatenation of beak and right wing
beakRightTail = [refBeak; refRightTail];
% Plot beak and right wing
brt = plot3(beakRightTail(:,1), beakRightTail(:,2), beakRightTail(:,3),'-ok');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Get corresponding left tail reference point
refLeftTail = [allPoints(row,10),... %x
allPoints(row,11), ... %y
allPoints(row,12)]; %z
% Get vertical concatenation of beak and right wing
beakLeftTail = [refBeak; refLeftTail];
% Plot beak and right wing
blt = plot3(beakLeftTail(:,1), beakLeftTail(:,2), beakLeftTail(:,3),'-ok');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Save frame
animBird = getframe(animPlot);
im = frame2im(animBird);
[imind,cm] = rgb2ind(im,256);
% Write to the GIF File
if row == 1
imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,filename,'gif','WriteMode','append');
end
% Delete lines
delete(blt), delete(brt), delete(blw), delete(brw);
row = row + 1;
end
end
6 Comments
Walter Roberson
on 19 May 2020
Is it possible that on the laptop you are writing to a network drive?
Answers (1)
Christopher Cochran
on 19 May 2020
4 Comments
Walter Roberson
on 11 Aug 2021
Edited: Walter Roberson
on 11 Aug 2021
That code always overwrites the GIF, since count = 1 followed by if count == 1 is always going to be true.
However, the original code had the problem that the isnan() could be true when row == 1, so testing row == 1 was not enough to know whether you had written anything to the gif. A counter initialized early on would be the fix for that.
KCE
on 16 Aug 2021
Edited: KCE
on 16 Aug 2021
I seem to be having this same issue and my count is initialized early on in my code.
clear all;
close all;
grid_box = [-5 50 -50 50]; %xmin xmax ymin ymax in km
for ik=1:361
r11(ik)=30.*cos(ik*pi/180.);
r12(ik)=30.*sin(ik*pi/180.);
end
for num=[20130519]
YYYYMMDD=num2str(num);
KXXX='KTLX';
count=0;
% Change directory to correct output file
cd = 'cd ';
%output = '/Users/djs78/Santellanes/RADAR/';
output = strcat('/stens1/s0/kce115/radar/data/',YYYYMMDD,'/',KXXX,'/');
fold = horzcat(output);
%fold = horzcat(output,'NoCirc');
chdir = horzcat(cd,output);
status = system(chdir);
fold
% Add current folder to MATLAB path
addpath(fold);
% List the cfrad.*.nc files in the specified folder
files = horzcat(fold,'/*.nc');
disp(files);
cfradfiles = dir(fullfile(files));
[nofiles na] = size(cfradfiles);
filecount = 0;
for i = 1:nofiles
% Identify each radar file individually
filen = cfradfiles(i).name;
% Read radar site from file
radname = filen(50:53);
file=filen;
if str2num(filen(16:19))>1500
count=count+1
%starts to grab info from the file
radname = file(50:53);
iSweepStart=ncread(file,'sweep_start_ray_index');
iSweepEnd=ncread(file,'sweep_end_ray_index');
elevs=ncread(file,'elevation'); %Netcdf radar data file
timesec = ncread(file,'time'); %time in seconds since beginning of volume scan
% %%%%%%%%%%%%%%%%%%
% %Prompts you to pick elevation angle sweep
% %%%%%%%%%%%%%%%%%
elevation_list = elevs(iSweepEnd);
ZH=ncread(file,'REF'); %
ZDR=ncread(file,'ZDR'); %Reads in the moment data from netcdf file
sys_fdp = ncread(file,'r_calib_system_phidp'); %system diff phasew
ray_n_gates=ncread(file,'ray_n_gates'); %Reads in some other
azis=ncread(file,'azimuth'); %important attributes of the
ranges_meters=ncread(file,'range'); %
range_first_gate_meters = ranges_meters(1); %Figures out range at first gate
gate_spacing = ranges_meters(50)-ranges_meters(49); %Should be 250 m for WSR-88Ds
tempn = ncread(file,'nyquist_velocity'); %nyquist velocity at every azimuth.
n_sweeps = numel(iSweepEnd); %
n_gates = ray_n_gates(iSweepStart+1); %Figure out the number of
n_azis = (iSweepEnd-iSweepStart)+1; %elements you have
%Choose elevation angle. 2 is 0.5 degree, 4 is 0.9 degree.
%djs test
nrays=size(elevation_list,1);
for nt=1:nrays;
testelev=abs(elevation_list(nt)-1.5);
if (testelev < 0.3)
pick_el=nt;
end
end
%disp(pick_el);
zz=pick_el; %Stupid line.
nyquist_vel = tempn(iSweepStart(zz)+1);
azimuths = zeros(n_azis(zz),1); %
zh=zeros(n_azis(zz),n_gates(zz))-200.0; %allocate some arrays
vel = zh;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%This part parses the CF-radial file into useable moment formats for the
%radar data moments: DATA(# azimuths, #range bins). Note that this is
%elevation sweep specific.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for xx=1:n_azis(zz)
%Parse out indices based on sweep number zz
start_ind = sum(ray_n_gates(1:iSweepStart(zz)+xx)) - n_gates(zz) + 1;
end_ind = sum(ray_n_gates(1:iSweepStart(zz)+xx));
%Read in Reflectivity factor at horizontal polarization ZH
zh(xx,1:n_gates(zz))=ZH(start_ind:end_ind);
temp=find(zh(xx,:) == -200);
zh(xx,temp) = NaN;
end
%populate the array of azimuths specific to the sweep
azimuths(:) = azis(iSweepStart(zz)+1:iSweepEnd(zz)+1);
%---Thanks to Scott Ellis (NCAR), from whom I stole the next few bits-------
%find the size of arrays
[m n]=size(zh);
%Get the arrays for polar coordinates
r2 = range_first_gate_meters:gate_spacing:range_first_gate_meters+gate_spacing*(n-1);
r2=r2./1000.0;
theta = -(azimuths-90.0).*pi./180;
%Take the polar coordinates and transform to a regular cartesian grid.
[r,theta]=meshgrid(r2,theta);
x1 = r .* cos(theta);
y1 = r .* sin(theta);
%-------end Scott Ellis stolen code----------------------------------
dualpol_colmap; %calls colormaps for dualpol variables.
scrsz = get(0,'ScreenSize');
scandatetime=ncread(file,'time_coverage_start')';
scandatetime=[scandatetime(1:10),' ',scandatetime(12:16),'Z'];
radarname = ncreadatt(file,'/','instrument_name');
degsym = ('^o');
titl=([num2str(elevation_list(zz),3),degsym,' ',radarname,' ',scandatetime]);
figure(1)
set(gcf, 'Position', [1,1,1000,900]);
pbaspect([2 2 2]);
varplotted = ('Z_H');
contourf(x1,y1,zh); axis(grid_box); shading interp; grid on; box on
np = 64;
z = zeros(np,np);
x = linspace(0.0,1.0,np);
x2d = repmat(x,np,1);
z = x2d;
colormap(zhmap)
colorbar
caxis([-20 75])
hTitle = title([varplotted, ' ', titl]);
hXLabel = xlabel('x (km)','fontsize', 20);
hYLabel = ylabel('y (km)','fontsize', 20);
set(gca,'XTick',[-75,-50,-25,0,25,50,75]);
set(gca,'XTickLabel',[-75,-50,-25,0,25,50,75]);
set(gca,'YTick',[-75,-50,-25,0,25,50,75]);
set(gca,'YTickLabel',[-75,-50,-25,0,25,50,75]);
set( gca, 'FontName','Helvetica','FontSize',20,'FontWeight','demi');
set([hTitle, hXLabel, hYLabel],'FontName','Helvetica');
set([hXLabel, hYLabel],'FontSize',20,'FontWeight','demi');
set(hTitle,'FontSize',20,'FontWeight','bold');
set(gca,'Color','k')
fig=1;
set(gcf,'color','w'); % set figure background to white
drawnow;
frame = getframe(fig);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
outfile = fullfile([file(7:14),varplotted,num2str(elevation_list(zz),3),'.gif']);
if count==1
imwrite(imind,cm,outfile,'gif','DelayTime',0.15,'loopcount',inf);
else
imwrite(imind,cm,outfile,'gif','DelayTime',0.15,'writemode','append');
end
end
end
end
See Also
Categories
Find more on Blue in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!