Help in creating video for particle trajectory

5 次查看(过去 30 天)
I have position vectors as a function of time., say, x=[1 X 100] , z=[1 X 100] , where time vector is t=[1 X 100]
plot(x, z) gives the trajectory of particle. However, I need to make a video showing particle movement while creating the trajectory. Also, I need to denote the partclie as a 'rod' and its varying orientation along the path. The orientation as a function of time could be found as angle=[1 X 100] .
Please suggest.

回答(2 个)

Image Analyst
Image Analyst 2023-4-30
See attached demo where I make a movie of a graph that changes over time. Adapt as needed.
  1 个评论
Ban
Ban 2023-4-30
In your code ('movie_made_from_surf.m'), if I replace surf(x,y,z) with plot(x1d(frameIndex), y1d(frameIndex)), it doesn't show any curve. Please help

请先登录,再进行评论。


Image Analyst
Image Analyst 2023-4-30
Try this:
% Demo to create a movie file from a quadratic curve plot, and then optionally save it to disk as an avi video file.
%==============================================================================================
% Initialization code
clearvars;
clc;
workspace;
fontSize = 15;
numberOfFrames = 100;
saveVideo = true;
t = linspace(0, 6, numberOfFrames);
hFigure = figure('Name', 'Demo by Image Analyst', 'NumberTitle', 'off');
%==============================================================================================
% Get a list of x and z coordinates for every time.
xVelocity = 1;
x = xVelocity * t;
z = -(t - 3) .^ 2 + 9;
% See the final plot.
% plot(x, z, 'b-');
% grid on;
% return;
%==============================================================================================
% Set up the movie structure.
% Preallocate movie, which will be an array of structures.
% First get a cell array with all the frames.
if saveVideo
allTheFrames = cell(numberOfFrames,1);
vidHeight = 344;
vidWidth = 446;
allTheFrames(:) = {zeros(vidHeight, vidWidth, 3, 'uint8')};
% Next get a cell array with all the colormaps.
allTheColorMaps = cell(numberOfFrames,1);
allTheColorMaps(:) = {zeros(256, 3)};
% Now combine these to make the array of structures.
myMovie = struct('cdata', allTheFrames, 'colormap', allTheColorMaps);
% Create a VideoWriter object to write the video out to a new, different file.
writerObj = VideoWriter('Delete Me.avi');
open(writerObj);
% Need to change from the default renderer to zbuffer to get it to work right.
% openGL doesn't work and Painters is way too slow.
set(gcf, 'renderer', 'zbuffer');
end
%==============================================================================================
% Create the movie.
% After this loop starts, BE SURE NOT TO RESIZE THE WINDOW AS IT'S SHOWING THE FRAMES, or else you won't be able to save it.
for frameIndex = 1 : numberOfFrames
cla reset;
% Enlarge figure to full screen.
% set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0, 1, 1]);
plot(x(1:frameIndex), z(1:frameIndex), 'b-', 'LineWidth', 3);
xlabel('x', 'FontSize', fontSize);
ylabel('z', 'FontSize', fontSize);
axis('tight')
xlim([0, 6]);
ylim([0, 9]);
grid on;
caption = sprintf('Frame #%d of %d, t = %.1f', frameIndex, numberOfFrames, t(frameIndex));
title(caption, 'FontSize', fontSize);
drawnow;
thisFrame = getframe(gcf);
% Write this frame out to a new video file.
if saveVideo
% writeVideo(writerObj, thisFrame);
myMovie(frameIndex) = thisFrame;
end
end
if saveVideo
% close(writerObj);
%==============================================================================================
% See if they want to replay the movie.
message = sprintf('Done creating movie\nDo you want to play it?');
button = questdlg(message, 'Continue?', 'Yes', 'No', 'Yes');
drawnow; % Refresh screen to get rid of dialog box remnants.
close(hFigure);
if strcmpi(button, 'Yes')
hFigure = figure('Name', 'Demo by Image Analyst', 'NumberTitle', 'off');
% Enlarge figure to full screen.
% set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0, 1, 1]);
title('Playing the movie we created', 'FontSize', fontSize);
% Get rid of extra set of axes that it makes for some reason.
axis off;
% Play the movie.
movie(myMovie);
close(hFigure);
end
%==============================================================================================
% See if they want to save the movie to an avi file on disk.
promptMessage = sprintf('Do you want to save this movie to disk?');
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Yes', 'No', 'Yes');
if strcmpi(button, 'yes')
% Get the name of the file that the user wants to save.
% Note, if you're saving an image you can use imsave() instead of uiputfile().
startingFolder = pwd;
defaultFileName = {'*.avi';'*.mp4';'*.mj2'}; %fullfile(startingFolder, '*.avi');
[baseFileName, folder] = uiputfile(defaultFileName, 'Specify a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(folder, baseFileName);
% Create a video writer object with that file name.
% The VideoWriter object must have a profile input argument, otherwise you get jpg.
% Determine the format the user specified:
[folder, baseFileName, ext] = fileparts(fullFileName);
switch lower(ext)
case '.jp2'
profile = 'Archival';
case '.mp4'
profile = 'MPEG-4';
otherwise
% Either avi or some other invalid extension.
profile = 'Uncompressed AVI';
end
% Commented out because it was already created above before the loop.
% writerObj = VideoWriter(fullFileName, profile);
% open(writerObj);
% Write out all the frames.
numberOfFrames = length(myMovie);
for frameNumber = 1 : numberOfFrames
writeVideo(writerObj, myMovie(frameNumber));
end
close(writerObj);
message = sprintf('Finished creating movie file\n %s.', fullFileName);
uiwait(helpdlg(message));
end
% If using Windows, open the folder and video.
if ispc && strcmpi(button, 'yes')
if ~strcmpi(pwd, folder)
% Open File Explorer to the folder where they saved the video, if it's not the current folder.
winopen(folder);
end
winopen(fullFileName);
end
end
uiwait(helpdlg('Done with demo!'));
  6 个评论
AkB
AkB 2023-5-1
编辑:Image Analyst 2023-5-2
Thanks. It worked. But my particle's leng keeps changing over time of its movement. Please see the attached image of the swimmers at 2 different time points. However my code looks okay. Could you please suggest why does it happen ?
workspace;
fontSize = 15;
numberOfFrames = length(t);
saveVideo = true;
hFigure = figure('Name', 'Demo by Image Analyst', 'NumberTitle', 'off');
if saveVideo
allTheFrames = cell(numberOfFrames,1);
vidHeight = 344;
vidWidth = 446;
allTheFrames(:) = {zeros(vidHeight, vidWidth, 3, 'uint8')};
allTheColorMaps = cell(numberOfFrames,1);
allTheColorMaps(:) = {zeros(256, 3)};
myMovie = struct('cdata', allTheFrames, 'colormap', allTheColorMaps);
writerObj = VideoWriter('Swimmers in waves.avi');
open(writerObj);
set(gcf, 'renderer', 'zbuffer');
end
for frameIndex = 1 : numberOfFrames
cla reset;
xp1=x_vec(1,frameIndex);
zp1=z_vec(1,frameIndex);
phi_p1=phi_vec(1,frameIndex);
xp6=x_vec(6,frameIndex);
zp6=z_vec(6,frameIndex);
phi_p6=phi_vec(6,frameIndex);
xp9=x_vec(9,frameIndex);
zp9=z_vec(9,frameIndex);
phi_p9=phi_vec(9,frameIndex);
lineSegmentLength = 0.1;
xt1 = xp1 + (lineSegmentLength/2) * sind(phi_p1);
xd1 = xp1 - (lineSegmentLength/2) * sind(phi_p1);
zt1 = zp1 + (lineSegmentLength/2) * cosd(phi_p1);
zd1 = zp1 - (lineSegmentLength/2) * cosd(phi_p1);
xt6 = xp6 + (lineSegmentLength/2) * sind(phi_p6);
xd6 = xp6 - (lineSegmentLength/2) * sind(phi_p6);
zt6 = zp6 + (lineSegmentLength/2) * cosd(phi_p6);
zd6 = zp6 - (lineSegmentLength/2) * cosd(phi_p6);
xt9 = xp9 + (lineSegmentLength/2) * sind(phi_p9);
xd9 = xp9 - (lineSegmentLength/2) * sind(phi_p9);
zt9 = zp9 + (lineSegmentLength/2) * cosd(phi_p9);
zd9 = zp9 - (lineSegmentLength/2) * cosd(phi_p9);
xleft= -0.1 ;
xright= 7 ;
zup= -0.95 ;
zdown= 0 ;
XX=linspace(xleft,xright,0.2*length(t));
ZZ=linspace(zup, zdown, 0.2*length(t));
[X,Y] = meshgrid(XX,ZZ);
T=t(1,frameIndex);
ux=w1*A*exp(k*Y)*cos(k*X-w1*T);
uz=w1*A*exp(k*Y)*sin(k*X-w1*T);
plot([xt1, xd1], [zt1, zd1], 'r', 'LineWidth', 3);
hold on
plot([xt6, xd6], [zt6, zd6], 'b', 'LineWidth', 3);
hold on
plot([xt9, xd9], [zt9, zd9], 'm', 'LineWidth', 3);
xlabel('x (m)', 'FontSize', 25);
ylabel('z (m)', 'FontSize', 25);
xlim([xleft, xright]);
ylim([zup, zdown+0.2]);
caption = sprintf('t = %.1f', t(frameIndex));
title([caption,' Seconds'], 'FontSize', 30);
set(gca,'FontSize',20);
drawnow;
thisFrame = getframe(gcf);
if saveVideo
% writeVideo(writerObj, thisFrame);
myMovie(frameIndex) = thisFrame;
end
end
Image Analyst
Image Analyst 2023-5-2
@AkB The above code doesn't run. Please attach the m-file with the paperclip icon.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by