I am just executing the following example to create a avi movie. After it's done, I cannot rename the file because Windows claims Matlab still has it "open".
Z = peaks;
surf(Z)
axis tight manual
ax = gca;
ax.NextPlot = 'replaceChildren';
loops = 40;
F(loops) = struct('cdata',[],'colormap',[]);
for j = 1:loops
X = sin(j*pi/10)*Z;
surf(X,Z)
drawnow
F(j) = getframe(gcf);
end
fig = figure;
movie(fig,F,2);
movie2avi(F, 'awesomeMovie', 'compression', 'None');
What's a good way to break that connection?
Thank you. Dave

 Accepted Answer

Adam
Adam on 12 Jul 2016
Are you using a pre 2010b version of Matlab?
If not then you should be able to use VideoWriter instead as movie2avi will be removed in a future release (according to the help).
e.g.
v = VideoWriter('newfile.avi');
% set properties of VideoWriter
open(v)
writeVideo(v,rand(300))
close(v)
This includes a clear instruction to close the VideoWriter.
I guess in your case just doing an
fclose( 'awesomeMovie' )
ought to work.

6 Comments

Guillaume
Guillaume on 12 Jul 2016
Edited: Guillaume on 12 Jul 2016
As far as I know, the only string input that fclose accepts is 'all', so I doubt fclose('awesomeMovie') will work.
However,
fclose('all')
ought to. It forces matlab to close all open file handles. If it still doesn't, I'm afraid the only way is to close matlab (or to switch to VideoWriter which is a better solution in the long run).
David Pesetsky
David Pesetsky on 12 Jul 2016
Edited: David Pesetsky on 12 Jul 2016
I'm using 2014b.
fclose insists on 'all', but even that doesn't close it:
>> fclose( 'awesomeMovie.avi' ) Error using fclose String argument must be 'all'.
I didn't know how to make the F structure into a movie, so movie2avi seemed easier. I'll switch over if someone can tell me how to convert F into an avi...
The documentation of VideoWwriter has an example that shows how to do nearly the exact same thing as your code.
As the frames are written in the loop you don't even need the F structure any more.
Adam
Adam on 12 Jul 2016
Yes, I forgot fclose takes a file id rather than a file name as its argument for a specific file.
Not sure movie2avi returns a pointer to an open file...
Ah! Guillaume, I just used VideoWriter to write a compact mp4 directly, rather than an avi and then compress it. Perfect. Thank you.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!