getframe produces empty structure when attempting to create animation

I am attempting to create a video from an animation of a heatmap of spatial data. Using getframe has worked for me previously when capturing figures of point data, but here I have seperate colour maps for bathymetry and the bincount of particles (as a pcolor plot) in the spatial grid so there are two axes (which I suspect is the issue). The code I used to create my heatmap figures follows:
blue_map = zeros(n,3); blue_map(:,3) = linspace(0,0.7,n)'; blue_map = flip(blue_map);
clear M
figure('Position',[1200 500 900 1200])
ax1 = axes;
bathy_contour = contourf(ax1,bathy_lon,bathy_lat,bathy,[0:5:100],'Edgecolor','none');
xlabel('Longitude','FontSize',12)
ylabel('Latitude','FontSize',12)
view(2)
ax2 = axes;
heatmap = pcolor(ax2,lon(1:end-1),lat(1:end-1),hist_heat_map(:,:,1));
set(heatmap,'EdgeColor','none');
caxis([0 200])
hold on
drouged1_fig = plot(drouged1{1,3},drouged1{1,2},'.r','MarkerSize',14,'DisplayName','SOSBU-MS-1');
drouged2_fig = plot(drouged2{1,3},drouged2{1,2},'.g','MarkerSize',14,'DisplayName','SOSBU-MS-2');
drouged3_fig = plot(drouged3{1,3},drouged3{1,2},'.y','MarkerSize',14,'DisplayName','SOSBU-MS-3');
linkaxes([ax1,ax2])
ax2.Visible = 'off'; ax2.XTick = []; ax2.YTick = [];
colormap(ax1,blue_map)
colormap(ax2,'autumn')
xlim([-5.6 -2.8])
ylim([53 55])
set([ax1,ax2],'Position',[.17 .11 .685 .815]);
cb1 = colorbar(ax1,'Position',[.05 .11 .0675 .815]);
cb1.Label.String='Depth below sea level (m)';
cb1.Label.FontSize=11;
cb2 = colorbar(ax2,'Position',[.88 .11 .0675 .815]);
cb2.Label.String='Number of particles per bathymetry grid cell';
cb2.Label.FontSize=11;
set(gcf,'Renderer','zbuffer');
hold off
legend([drouged1_fig drouged2_fig drouged3_fig])
M = getframe(gcf);
for i = 2:size(hist_heat_map,3)
heatmap.CData = hist_heat_map(:,:,i);
drouged1_fig.XData = drouged1{int(i),3};
drouged1_fig.YData = drouged1{int(i),2};
drouged2_fig.XData = drouged2{int(i),3};
drouged2_fig.YData = drouged2{int(i),2};
drouged3_fig.XData = drouged3{int(i),3};
drouged3_fig.YData = drouged3{int(i),2};
set(gcf,'Renderer','zbuffer');
legend([drouged1_fig drouged2_fig drouged3_fig])
drawnow
M(j) = getframe(gcf);
end
This code performs without errors and generates what I want to see, which is the heatmap pcolor plot overlaying the bathymetry contour plot. However, the fields cdata and colormap in structure M hold empty cells when the loop has executed, so I have nothing to write a video with.
Does anyone have a suggestion as to how I can capture everything in my figure window to create an animation with? Any help would be appreciated.
Thank you for your time.

4 Comments

We cannot run you code due to the missing inputs.
Sometimes it helped magically to insert a pause(0.02) before getframe.
What is "int()" in:
drouged1_fig.XData = drouged1{int(i),3};
? This is usually a symbolic integral...
I recommend against continually calling gcf() . The current figure can change under various circumstances, including that the user happens to click something else in order to move it out of the way to see what is happening.
I recommand that you record the output of the figure() call and use that has the figure handle instead of gcf()
Thanks everyone for your comments, it of course turned out to be I forgot to change the indexing variable from j to i when I copied the getframe line from another part of my code (thanks Walter). Jan: int was to change the loop index to an integer, for some reason the code didn't accept i within the curly brackets before I did that. I guess calling double() or single() would have worked as well. Walter: thanks for the suggestion, I'll look at changing to that

Sign in to comment.

 Accepted Answer

M(j) = getframe(gcf);
You are looping over i but indexing j

1 Comment

Well that's embarassing... I don't even know why I changed to i, I normally use j or k. That works perfectly now, thanks Walter

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2021b

Community Treasure Hunt

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

Start Hunting!