create .gif animation with mixed subplots of grayscale and color figures

4 views (last 30 days)
Hi All,
I am running into difficulties of creating a matlab gif animation with two subplots running simultaneously, one grayscale matrix image and one running sinusoidal curve. I can get them to run separately, but don't know how to put them together using imwrite function. Below is what I have so far.
sinus curve:
T = linspace(0, 100, 101);
Y = sin(T/10*pi);
Z = cos(T/10*pi);
subplot(2,1,1)
plot(T, Y, 'LineWidth', 2)
hh1(1) = line(T(1), Y(1), 'Marker', '.', 'MarkerSize', 20, 'Color', 'b');
subplot(2,1,2)
plot(T, Z, 'LineWidth', 2)
hh2(1) = line(T(1), Z(1), 'Marker', '.', 'MarkerSize', 20, 'Color', 'r');
xlabel('time (sec)')
ylabel('P (pascal)')
% h = plot(hSubPlotAxes, NaN, NaN);
axis([min(T) max(T) min(Y) max(Y)]);
for jj = 1:length(T)
set(hh1, 'XData', T(1:jj), 'YData', Y(1:jj));
set(hh2, 'XData', T(1:jj), 'YData', Z(1:jj));
frame = getframe(gcf);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if jj == 1;
imwrite(imind,cm,'test_sinus.gif','gif','Loopcount',inf,'DelayTime',0.01);
else
imwrite(imind,cm,'test_sinus.gif','gif','WriteMode','append','DelayTime',0.01);
end
end
and the grayscale gif movie
numImages = 20;
coronalSliceNum = 193;
viewWindow = [-300 - (1700/2), -300 + (1700/2)];
cView = zeros(414, 429, numImages);
% Load images, get coronal slices
for idx = 1:numImages;
phaseName = fullfile(imageFolder,sprintf('model_cont_phase_%d',idx));
vol_img = metaImageRead(phaseName);
cView(:,:,idx) = imrotate(squeeze(vol_img(coronalSliceNum,:,:)),90);
end
for idx = 1:numImages
% Convert to index image using window/level
frame = mat2gray(cView(:,:,idx),viewWindow);
[frameIndexed,frameMap] = gray2ind(frame,256);
% Write .gif. Set delay time here
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
end
any help is appreciated !!!
  2 Comments
Geoff Hayes
Geoff Hayes on 10 Jul 2015
derbruin - you need to define how the two are to be run together. When you update the sine curve, which image do you want to show? Are both still in separate subplots?
derbruin
derbruin on 10 Jul 2015
Hi Geoff, thanks for looking into this. The sine curve and the image are in separate subplots. Perhaps I have written this somewhat sloppily, but I'd like to have the length of the sine array to be the same as the number of images. So each index from one subplot corresponds to the index in the other subplot on a one-to-one basis.

Sign in to comment.

Answers (1)

Geoff Hayes
Geoff Hayes on 13 Jul 2015
derbruin - if you know the number of images that you have, numImages, then why not combine the two loops as
numImages = 20;
coronalSliceNum = 193;
viewWindow = [-300 - (1700/2), -300 + (1700/2)];
cView = zeros(414, 429, numImages);
T = linspace(0, 100, numImages);
% etc.
Now just have the one loop where you update the subplot with the sine curve and update the other subplot with the image. You will probably need to use a call to pause in order to halt execution temporarily so that the subplots refresh. Something like
for k=1:numImages
% update sine subplot
% update image subplot
% halt execution for half a second
pause(0.5);
end

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!