How to animate an ImageSC plot in real-time

21 ビュー (過去 30 日間)
Edward Keavney
Edward Keavney 2022 年 8 月 8 日
回答済み: Varun 2023 年 9 月 1 日
I have an array 26X26 of velocity data that I have plotted as an ImageSC plot. The Xaxis of the ImageSC plot corresponds to time and the Yaxis corresponds to depth. The Xaxis corresponds to the velocity over a 26 second period, and up the Yaxis is the velocity at mm intervals over a flow depth of 2.6 cm. Is there a way where I can animate the plot so that the animation is in real-time, whereby the first second of velocity data is plotted over the full flow depth and then so on and so on for all 26 seconds?
Additionally what would be the best way to export the animation (if possible) whilst maintaining quality for a presentation?
Any help would be greatly appreciated - thanks in advance!

回答 (1 件)

Varun 2023 年 9 月 1 日
As per my understanding, you have a 26*26 size matrix with velocity data and you wish to plot them using “imagesc, and then export an animation showing every plot as a frame in it.
The 26 plots can be generated iteratively using a “for” loop, which plots data from the “velocity” matrix one row at a time. So, in each iteration, one plot is generated.
To store and export the animation, “VideoWriter” can be used. The frame rate can be set to 1 frame/second for convenience.
outputVideo = VideoWriter('velocity_animation.mp4', 'MPEG-4');
In the “for” loop, each frame can be written into the video using the “writeVideo” function. So, the overall program could looking something like this:
% Assuming you have a 26x26 velocity data array called 'velocityData'
% Create a figure and axes for the animation
% Assuming you have a 26x26 velocity data array called 'velocityData'
% Create a VideoWriter object to save the animation
outputVideo = VideoWriter('velocity_animation.mp4', 'MPEG-4');
% Create a figure and axes for the animation
axesHandle = axes;
% Set the limits of the axes based on your flow depth and time period
%Custom setting as needed
% Loop through each time step and update the plot
for t = 1:timePeriod
% Get the velocity data for the current time step
velocity = velocityData(:, t);
% Plot the velocity data as an imagesc plot
% Add labels and title
xlabel('Time (s)');
ylabel('Depth (cm)');
title(['Velocity at t = ' num2str(t) ' seconds']);
% Write the current frame to the video file
frame = getframe(gcf);
writeVideo(outputVideo, frame);
% Close the video file
You may refer to the following documentation to know more about:
Hope this helps!


Help Center および File ExchangeAnimation についてさらに検索





Community Treasure Hunt

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

Start Hunting!

Translated by