MATLAB animate line plots from data

2019-04-13 00:00发布

问题:

I have 30 experimental trials that I logged on a DAQ and I read these in to MATLAB using xlsread.

I have a plot now where I show all 30 trials on one figure.

What I want to do is create a movie where it shows them plotting one by one.

Really I have a total of 60 line plots (30 inputs and 30 outputs), so it would be great if I could show the input and output for one, then the next, etc.

Right now my code is similar to this:

In_1 = xlsread(filename.xls, #1);
In_2 = xlsread(filename.xls, #2);
...

Out_1 = xlsread(filename.xls, #1);
Out_2 = xlsread(filename.xls, #2);
...
plot(t, In_1,'r')
plot(t, Out_1)
plot(t, In_2,'r')
plot(t, Out_2)
...

I've seen plenty of examples using getframe and movie. This does not help me, because I am plotting DATA. Not a function that is evaluated over a time interval. I am trying to plot 30 trials of data.

回答1:

Use avifile to create an AVI file and then addframe to capture each plotted graph you plot and convert it to a frame. I've also taken the liberty to add subplots to keep both plots in the same figure, and rewrite your code into a loop.

Here's my (revised) suggested solution:

aviobj = avifile('output.avi');                     %# Create an AVI file
hh = figure;
for i = 1:30
    figure(hh)                                      %# Reuse the figure
    subplot(2, 1, 1)
    eval(['plot(t, In_', num2str(i), ', ''r'')']);  %# Plot i-th input
    %# axis([ something ]);
    subplot(2, 1, 2)
    eval(['plot(t, Out_', num2str(i), ', ''r'')']); %# Plot i-th output
    %# axis([ something ]);
    aviobj = addframe(aviobj, getframe(hh));        %# Convert to a frame
end
aviobj = close(aviobj);                             %# Close the AVI file

You can fiddle with the avifile options to control the quality of the generated video.

Alos note that each plot sets the x and y axes according to the plotted values. If you want to keep the animation "smooth", you have to force the axes to remain constant for each graph, after every plot, using the axis command.



回答2:

Here is a simple example that should cover your needs.

Note: This will create an .avi file you can view in most multimedia players afterwards.

%% Open avi file
aviobj = avifile('fileame.avi','compression','none','fps',framerate);

%% Add one frame at a time
for frame = 1:length(frames)             

    h = figure(1);
    % Plot what ever you want here...

    % Fetch frame
    aviobj = addframe(aviobj, getframe(h));
end

%% Close video
aviobj = close(aviobj);

If you want the input and output plotted on two separate axes you can use subplot.

h = figure(1);
subplot(2,1,1)
% Plot input here...
subplot(2,1,2)
% Plot output here...


回答3:

The easiest way would be to use the getframe command in a for loop to put each plot in a frame and then play them back using the movie command, this has some advantages (you can export the result as an avi, for instance) but it's quite slow sometimes, if you want to simply view the animation in matlab and need it to be somewhat faster you can refresh the data in the plot window and redraw it, it's a little more complex but it's explained quite well here Link.

Also, type help getframe and help movie in matlab for some explanations and examples as to how best use those commands.

Hope this helps.