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

5 vues (au cours des 30 derniers jours)
derbruin le 8 Juil 2015
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);
plot(T, Y, 'LineWidth', 2)
hh1(1) = line(T(1), Y(1), 'Marker', '.', 'MarkerSize', 20, 'Color', 'b');
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;
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);
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);
imwrite(frameIndexed, frameMap,fullfile(outputFolder,outputFilename),'gif','DelayTime', 0.15, 'WriteMode','append');
any help is appreciated !!!
  2 commentaires
derbruin le 10 Juil 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.

Connectez-vous pour commenter.

Réponses (1)

Geoff Hayes
Geoff Hayes le 13 Juil 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


En savoir plus sur Animation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by