creating an animated video by rotating a surface plot

43 visualizaciones (últimos 30 días)
Optical_Stress
Optical_Stress el 27 de Abr. de 2018
Respondida: Jaswanth el 15 de Oct. de 2024
I'm struggling to get a consistent animation that has fixed axis when rotating my surface plot. Two of the problems i have faced are:
1) the data size of my surfplot changes as the rotation angle changes. 2) the color of the surface plot is not retained when i store the data in a structure. 3) So far I have compiled this:
number_of_frames = 360;
n=1;
for i = 1:10:number_of_frames
hSurface = surf(velocityimg);
grid off
rotate(hSurface,[0 0 1],i);
newdata(n).x = hSurface.XData;
newdata(n).y = hSurface.YData;
newdata(n).z = hSurface.ZData;
surf(newdata(n).x,newdata(n).y,newdata(n).z);
grid off
hAxes = gca;
hAxes.XRuler.Axle.LineStyle = 'none';
axis off
axis vis3d
colormap(jet)
g(n) = getframe(gcf);
g(n) = frame2im(g(n));
n=n+1;
end
%%make video
v = VideoWriter('rotating.avi');
v.FrameRate = 10;
v.Quality = 65;
open(v);
for i =1:length(g)
frame = g(i).cdata;
writeVideo(v,frame);
end
close(v);
winopen('rotating.avi');
  1 comentario
Sky Nelson-Isaacs
Sky Nelson-Isaacs el 6 de Nov. de 2019
I have the same issue. How do I keep the axes consistent as I rotate the frame and take snapshots?

Iniciar sesión para comentar.

Respuestas (1)

Jaswanth
Jaswanth el 15 de Oct. de 2024
Hi,
To create an animated video by rotating a surface plot in MATLAB while ensuring the axis remains consistent and the color is retained throughout the animation, you can keep the axis limits stable using “axis tight” command, and “axis vis3d” ensures the aspect ratio remains fixed during the 3D rotation.
As you rotate the surface plot, adjust the view angle incrementally, capturing each frame with “getframe”. These frames are then compiled into a video using “writeVideo”.
Please refer to the following example code with assumed data:
% Create sample data for the surface plot
[X, Y] = meshgrid(-5:0.5:5, -5:0.5:5);
Z = sin(sqrt(X.^2 + Y.^2));
% Set up the figure and surface plot
figure;
hSurface = surf(X, Y, Z);
shading interp; % Enable smooth color transitions
colormap jet; % Ensure a consistent color scheme
axis tight; % Keep axis limits stable
axis vis3d; % Fix the aspect ratio for 3D rotation
% Set up the video writer
v = VideoWriter('rotating_surface_plot.avi');
open(v);
% Specify the number of frames for the animation
numFrames = 360;
% Rotate the surface plot and capture frames
for k = 1:numFrames
% Change the view angle
view([k, 30]);
% Capture the current frame
frame = getframe(gcf);
writeVideo(v, frame);
end
% Complete the video writing process
close(v);
% Close the figure display
close(gcf);
Kindly refer to the following MathWorks documentation to know more about the functions discussed above:
I hope the solution provided above is helpful.

Categorías

Más información sobre Animation en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by