Changing width of color bar and re-positioning it in movie frames

4 views (last 30 days)
Hello. I have data I'm using to make a movie from surface plots. I'm trying to add a fixed color bar with a width about 1/2 times the default, but adjusting the width causes the color bar to overlap each plot. My code only ends up correcting this for the first frame, but does not correct it for the rest of them. Any suggestions would be greatly appreciated.
set(gcf,'visible','off')
mov(pf) = struct('cdata',[],'colormap',[]);
for p = 1:pf
surf(X(:,:,p),Y(:,:,p),P(:,:,p));
grid('off')
xlabel('x(m)');
ylabel('y(m)');
zlabel('|Grad(P)| (Pa/m)');
axis([x0 xf y0 yf z0 zf])
titleInfo = cell(3,1);
titleInfo{1,1} = '|GradP| vs x and y';
titleInfo{2,1} = ['U = ',num2str(u,'%3.2f'),' (m/s)'];
titleInfo{3,1} = ['t = ',num2str(t(p),'%3.2f'),' (s)'];
title(titleInfo);
view(50,45)
colormap jet
caxis manual
caxis([z0 zf])
*cbar = colorbar;
ax = gca;
axpos = ax.Position;
cpos = cbar.Position;
cpos(3) = 0.5*cpos(3);
cbar.Position = cpos;
ax.Position = axpos;*
mov(p) = getframe(gcf);
end
close('all')
set(gcf,'visible','on')

Accepted Answer

Mike Garrity
Mike Garrity on 28 Mar 2016
I think that the colorbar and axes positions aren't really stable at the point that you're changing the Position. Possibly because the figure is Visible='off', but I'm not sure about that. You can probably get things to be a bit more stable if you insert a drawnow after call the colorbar.
But, when I'm doing an animation like this, I usually pull the plotting function out of the loop and use the returned handle to update the data. The reason for this is that then all of the axes positioning is only happening once, not every frame. That tends to make things a lot more stable.
For your example, that might look something like this:
set(gcf,'visible','off')
mov(pf) = struct('cdata',[],'colormap',[]);
% Initialization
h = surf(X(:,:,1),Y(:,:,1),P(:,:,1));
grid('off')
xlabel('x(m)');
ylabel('y(m)');
zlabel('|Grad(P)| (Pa/m)');
axis([x0 xf y0 yf z0 zf])
view(50,45)
colormap jet
caxis manual
caxis([z0 zf])
cbar = colorbar;
ax = gca;
axpos = ax.Position;
cpos = cbar.Position;
cpos(3) = 0.5*cpos(3);
cbar.Position = cpos;
ax.Position = axpos;
% Animation
for p = 1:pf
h.XData = X(:,:,p);
h.YData = Y(:,:,p);
h.ZData = P(:,:,;);
titleInfo = cell(3,1);
titleInfo{1,1} = '|GradP| vs x and y';
titleInfo{2,1} = ['U = ',num2str(u,'%3.2f'),' (m/s)'];
titleInfo{3,1} = ['t = ',num2str(t(p),'%3.2f'),' (s)'];
title(titleInfo);
mov(p) = getframe(gcf);
end
close('all')
set(gcf,'visible','on')
  4 Comments
Mike Garrity
Mike Garrity on 28 Mar 2016
Really? If you run it with the figure visible, can you see the title changing?
Matt Karlson
Matt Karlson on 28 Mar 2016
I realized I forgot to call the title in the loop. Once I did that it appeared in the frames, but now it's partly cut off from each frame. Is that just from the view settings I specified?

Sign in to comment.

More Answers (0)

Categories

Find more on Animation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!