MATLAB Answers

0

Adding subplots to animated gif file

Asked by mashtine on 16 Oct 2014
Latest activity Commented on by derbruin
on 8 Jul 2015
Hi all,
I have the following code to create a gif from my data plots and I would like to do this with subplots for other parameters. The code is a bit messy but I am not sure how to work the handles for the subplots to have the playback synchronized for each plot:
DATASET1 = tower_pres_allyrs(1:100,1);
DATASET2 = tower_pres_allyrs(1:100,2);
h = plot(NaN,NaN);
axis([min(DATASET1) max(DATASET1) min(DATASET2) max(DATASET2)]);
for ii = 1:length(DATASET1)
pause(0.1)
set(h, 'XData', DATASET1(1:ii), 'YData', DATASET2(1:ii));
drawnow
frame = getframe(1);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if ii == 1;
imwrite(imind,cm,'filename.gif','gif','Loopcount',inf,'DelayTime',0.01);
else
imwrite(imind,cm,'filename.gif','gif','WriteMode','append','DelayTime',0.01);
end
end
Thanks!

  0 Comments

Sign in to comment.

1 Answer

Answer by Geoff Hayes
on 17 Oct 2014
Edited by Geoff Hayes
on 22 Oct 2014
 Accepted Answer

Masao - what do you mean by I would like to do this with subplots for other parameters? As your above code doesn't reference subplots, how are you currently using subplots?
If hSubPlotAxes is the handle to the axes of any subplot, then your above code could become
h = plot(hSubPlotAxes,NaN,NaN);
axis(hSubPlotAxes,[min(DATASET1) max(DATASET1) min(DATASET2) max(DATASET2)]);
for ii = 1:length(DATASET1)
pause(0.1)
set(h, 'XData', DATASET1(1:ii), 'YData', DATASET2(1:ii));
drawnow
frame = getframe(hSubPlotAxes);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if ii == 1;
imwrite(imind,cm,'filename.gif','gif','Loopcount',inf,'DelayTime',0.01);
else
imwrite(imind,cm,'filename.gif','gif','WriteMode','append','DelayTime',0.01);
end
end
So we just use the handle to the subplot axes for whenever we wish to plot, set the axis limits, or getframe. You could the above code in a function and just pass the data, handle, and filename, so as to re-use the same code for each subplot.
EDIT
The line to call getframe has been updated to remove the second parameter.

  20 Comments

Hi, Geoff:
just following up on Masao's question. I would like to simultaneously update a curve and a picture side by side. So far, I can update the picture. My question is how can I update the two together?
for idx = 1:numImages
frame = mat2gray(cView(:,:,ind),lungWindow);
[frameIndexed,frameMap] = gray2ind(frame,256);
if idx == 1
imwrite(frameIndexed, frameMap,fullfile(outputFolder,outputFilename),'gif','DelayTime', 0.15, 'LoopCount', inf);
else
imwrite(frameIndexed, frameMap,fullfile(outputFolder,outputFilename),'gif','DelayTime', 0.15, 'WriteMode','append');
end
Any help is appreciated!
Derbruin - where is the code to update the curve or even plot the image? Does the curve appear on the same axes as the image or on a different one? Please provide more information. As well, you may just want to post this as a separate question since it is distinct from Masao's question.
hi Geoff,
thanks for looking into this for me. I posted my question using this title "create .gif animation with mixed subplots of grayscale and color figures". Appreciate your help!

Sign in to comment.