0

I want to generate a movie of time-dependent simulation results in Matlab by inserting the following commands at the end of the program in the plot statement. However, the figure stops at the first frame, but if I comment out the last two lines about the movie, the code shows the time-dependent result during running but does not generate any movie.

I understand that in Matlab there are several ways to do that, but what I want is a simplest way. That is to insert the two line, actually one line, which should be something like:

eval(['print -dpict ','temp',num2str(is)]);

in the plot statement to print out each frame as a picture file, and then assemble them into a movie. My plotting code is as follows. Could you please suggest me how to fix this? Thank you very much.

time=time+dt                   % plot the results
uu(1:nx+1,1:ny+1)=0.5*(u(1:nx+1,2:ny+2)+u(1:nx+1,1:ny+1));
vv(1:nx+1,1:ny+1)=0.5*(v(2:nx+2,1:ny+1)+v(1:nx+1,1:ny+1));
for i=1:nx+1,xh(i)=dx*(i-1);end;     for j=1:ny+1,yh(j)=dy*(j-1);end
hold off,contour(x,y,flipud(rot90(r))),axis equal,axis([0 Lx 0 Ly]);
hold on;quiver(xh,yh,flipud(rot90(uu)),flipud(rot90(vv)),'r');
pause(0.01);  
% movie_str = ['print -dpict','temp',num2str(is)]; % generate a movie
% eval(movie_str); pause(0.01);
end
Enter
  • 101
  • 2
  • This [my old answer](https://stackoverflow.com/a/42454555/4806927) could be of help to you – il_raffa Jan 03 '18 at 06:55
  • @horchler I understand that in Matlab there are several ways to do that, but what I want is the simplest way. That is to insert the two line, actually one line: `eval(['print -dpict ','temp',num2str(is)]);pause(0.01);` in the plot statement to print out each frame as a picture file, and then assemble them into a movie. Therefore, I don't think this question already has an answer after reviewing the answer. – Enter Jan 04 '18 at 12:38
  • I'd argue that `VideoWriter` is the simplest and most standard way to produce movies in Matlab. Your method doesn't produce a movie but rather a set of still images with no file extension. In addition, there is no "-dpict" option for `print` (at least not in R2017b). Lastly, rather than using `eval`, you should be using the [functional syntax](https://www.mathworks.com/help/matlab/matlab_prog/command-vs-function-syntax.html) for `print`, e.g., `print('-dpng',['temp' num2str(is) '.some_extension'])`. – horchler Jan 04 '18 at 17:49
  • @horchler, now I knew I was using `eval` function to construct and pass each file name to a function using command syntax, which is not recommended. Could you suggest how can I specify a directory in `eval(['print -depsc','temp',num2str(is)])`, if I really want to use it. Thanks. – Enter Jan 05 '18 at 10:25

1 Answers1

0

YOu can consider making a .gif file of your results. You may check this code:

h = figure;
axis tight manual % this ensures that getframe() returns a consistent size
filename = 'testAnimated.gif';
for n = 1:0.1:5
    % Draw plot for y = x.^n
    x = 0:0.01:1;
    y = x.^n;
    plot(x,y)
    drawnow
    % Capture the plot as an image
    frame = getframe(h);
    im = frame2im(frame);
    [imind,cm] = rgb2ind(im,256);
    % Write to the GIF File
    if n == 1
        imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
    else
        imwrite(imind,cm,filename,'gif','WriteMode','append');
    end
end

Refer this link: https://in.mathworks.com/matlabcentral/answers/94495-how-can-i-create-animated-gif-images-in-matlab

Siva Srinivas Kolukula
  • 1,251
  • 1
  • 7
  • 14