How can I write this animated figure as a movie?

23 views (last 30 days)
Peter Beringer
Peter Beringer on 12 Oct 2020
Commented: Peter Beringer on 14 Oct 2020
This is a scaled-down model of a larger bit of code I have been trying to write to generate an animated plot of audio measurements taken in a matrix of 10 x 10 microphone positions - 10 simulatneous channels per each of the 10 positions (well, 11 channels were recorded at each position, but I only need 10 of them, hence the indexing in the example datacall format from the real code). Was unable to get it to work properly at "full-scale" (no errors, but an almost "jammed" figure, so probably wildly inefficient processing), so made a small model of it to demostrate what I'm trying to achieve. Further to saving the animated plot as a movie, if anyone has any other suggestions on how to improve the code, I'd be very grateful to hear them.
I've tried the things suggested in the documentation for writeAnimation and fanimator, but I don't think I'm giving those functions the correct type of figure/data to write.
Many thanks in advance!
% "SCALE MODEL" of 10x10 PLOTTING CODE
% Build random matrices for example purposes - i.e., 5 "channels" of audio, each
% 20 "samples" long. Each five channel signal "played" from five
% positions, resulting in a 5 x 5 matrix of microphones/measurements/audio
% vectors.
fs = 5; % Otherwise 48000 for audio measurements.
a = -1; % Generate values between -1 and 1 to simulate audio signals.
b = 1;
Position1 = a + (b - a) .* rand(20,5); % Would call audio data here.
Position2 = a + (b - a) .* rand(20,5); % Format: Pos01audio = Pos01audiodata.audio(:,1:10);
Position3 = a + (b - a) .* rand(20,5); % Should audio vectors be tranposed into rows?
Position4 = a + (b - a) .* rand(20,5);
Position5 = a + (b - a) .* rand(20,5);
[NumSamples] = size(Position1(:,1)); % Get length of audio vectors from any; all measurements are the same length; 4secs or 4*fs.
NumSamples = NumSamples(1,1);
for i = 1 : NumSamples % Length of audio file in samples.
PlotMatrix = [Position1(i,:); Position2(i,:); Position3(i,:); Position4(i,:); Position5(i,:)]; % Concatenate arrays into matrix to be rewritten in 'for' loop.
OutputPlot = surf(PlotMatrix);
grid on
pause(1/fs) % Make pause equivalent to sample rate.
drawnow
end

Answers (1)

KSSV
KSSV on 12 Oct 2020
  10 Comments
Peter Beringer
Peter Beringer on 14 Oct 2020
Thanks a lot! It got me there in the end. I intend to save as AVI movie, but that looks simple enough to implement from where I am now. The main issue I was having was calling the "axes handle" of the figure being created by the 'for' loop. But now that I've got the image capture working fine, and AVI will be simple.
Looking at the code, I get the impression I have a few unnecesary (or misinterpreted) lines, possibly making it inefficient. Do you have any quick thoughts on that?
Thanks.
Here is the 'for' loop code updated to capture image frames ...
for i = 1 : NumSamples % Length of audio file in samples.
PlotMatrix = [Position1(i,:); Position2(i,:); Position3(i,:); Position4(i,:); Position5(i,:)]; % Concatenate arrays into matrix to be rewritten in 'for' loop.
OutputPlot = surf(PlotMatrix);
grid on
pause(1/fs) % Make pause equivalent to sample rate.
drawnow
PlotHandle = gcf;
ImageFrames = getframe(PlotHandle);
im = frame2im(ImageFrames);
[imind, cm] = rgb2ind(im,256);
% Write GIF file
if i == 1
imwrite(imind, cm, 'AnimatedFigure.gif', 'gif', 'Loopcount', inf);
else
imwrite(imind, cm, 'AnimatedFigure.gif', 'gif', 'WriteMode', 'append');
end
end

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!