MATLAB Answers

How can I make a file be automatically closed after ending a running matlab script...

조회 수: 25(최근 30일)
Chris
Chris 27 Jun 2013
Hello,
I've been working on a script that uses the VideoWriter class in order to write each frame generated to an avi file. Currently, I have to use control + C to end the script. This means that the file is not properly closed after the script ends, so if I open it in a video player I get an error due to the lock on the file. I have to manually execute a close statement on the file each time in matlab, which is tedious.
I've looked into the following possibilities:
*I tried using CloseRequestFcn to make the script end when the X button is clicked in the topright of the window. I can close the file here. Unfortunately, however, there does not appear to be a command that ends a script. "quit" exits matlab altogether, which forces me to restart matlab and wait for the IDE to reload.
*As best as I can tell keyboard handling is done with call back functions. I'm not really sure how you can tell the main script to end its loop from a callback function.
Thanks in advance.

채택된 답변

Walter Roberson
Walter Roberson 27 Jun 2013
Have your loop check a flag to see if it should exit. Have one of the callbacks set the flag.
while true
drawnow();
should_exit = get(handles.SOMEHANDLE, 'value');
if should_exit; break; end
.......
end
  댓글 수: 1
Chris
Chris 27 Jun 2013
Sorry, not sure if I should have posted my question here...It showed up as another answer below.

댓글을 달려면 로그인하십시오.

추가 답변(2개)

Chris
Chris 27 Jun 2013
Is get with a handle a way of passing variables across scripts/functions/files? I know how to do this kind of thing in C++/Java but am not as well versed with Matlab.
  댓글 수: 2
Chris
Chris 28 Jun 2013
So in order to use the code above, you would essentially associate variables with a UI object and then use get to get them?
If I'm just rendering to a window with plot and don't have any actual UI controls, would I need to do anything special to use this?

댓글을 달려면 로그인하십시오.


Tom
Tom 28 Jun 2013
What conditions do want to satisfy to make it close? Or rather, what is preventing you from using close(vidObj) ?

Community Treasure Hunt

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

Start Hunting!

Translated by