Run a section of code, pause, close figures and proceed to next section

조회 수: 2 (최근 30일)
Andrew Chen
Andrew Chen 2017년 1월 29일
댓글: Iddo Weiner 2017년 2월 1일
Hello, I am trying to beautify my code and it would be great if I could make my code run as described in the question. My most recent attempt looked something like this:
%%Section1
X = [...];
Y = [...];
h(1) = figure
plot(X)
hold on
plot(Y)
pause;
close figure 1
%%Section2
Not all sections have figures however (some only display text in the command window) but for some reason, when I would hit a key to advance past the pause, the figure would close and no code would be displayed from the next section. The run button in the editor tool bar said run (not pause) but when I clicked on it, the entire code would not run. I would then close matlab and would get a message that says something like matlab is running something and if you close now whatever it is doing would be incomplete.
Basically, I would greatly appreciate it if any of you know how to pause code at the end of a section then, upon some input, close any figures at the end of that section and advance to the next section. Thanks for your time!

답변 (1개)

Iddo Weiner
Iddo Weiner 2017년 1월 29일
Have you tried running sections (mark a section by starting it with %%+enter) separately with ctrl+enter?
then you can plant:
close all
at the end of the section and basically you get what you're looking for, if I understand you correctly..
  댓글 수: 3
Andrew Chen
Andrew Chen 2017년 1월 30일
So I am replying directly to you Sean since I apparently can only accept Iddo's answer and not reply. I can do that but I was looking for a more stream line solution. As you can see from my code sample above I cam marking my sections with two percent signs but I would like a solution where I can hit run, the code will pause at some point that I mark. Then I can press a button and the code will continue to the next pause. I have tried using the close all command but I have seen others comment (on other places on the internet) that the close all command will also close the matlab gui. Perhaps that is the reason that I am experiencing the crashes I described above. Regardless, if you or anyone has any other methods to achieve the effect I am looking for, that would be much appreciated.
Iddo Weiner
Iddo Weiner 2017년 2월 1일
Andrew, if you know in advance where you want to pause, and don;t want to use %% you can use matlab's stop ( doc stop). If you don't know this in advance, there might be a different option - I found this file in the file exchange, it's supposed to create a pause button, hope it helps:
http://www.mathworks.com/matlabcentral/fileexchange/6060-pausebutton
Regarding closing figures - if you don't want to use close all, there are other, more specific ways to do so. For instance - you can go: close(gcf) to close the last figure produced. You can also give handles to all your figures and use those to close specific figures. Check out Matlab's close documentation ( doc close).
Hope this is helpful!

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

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by