How to manualy break a for loop but continue with the rest of matlab code execution?
조회 수: 23 (최근 30일)
이전 댓글 표시
How to manually break a for loop in matlab during execution so that the program get out of loop and continue the rest of the code after the for loop?
I define a for loop for 2000 iterations. Each iteration needs to perform an expensive function evaluation for a few minutes which give me a new point (x,y) to dynamically update my line plot. I am going to dynamically plot (x,y) points. Whenever the user decides not to continue with the for loop based on the shape of the dynamic plot, he /she should be able to break the loop and continue with next piece of codes comes after the loop. Note that Ctr+C just kills all the codes.
댓글 수: 1
David Young
2014년 7월 28일
Is it acceptable for the user to be asked whether or not to continue each time the end of the loop is reached?
채택된 답변
Patrik Ek
2014년 7월 28일
편집: Patrik Ek
2014년 7월 28일
It is possible, but not trivial. Matlab figures is a one type of gui. For these there are some defined processes that can do what you want to. Speaking lower level, you can say that you pushes and pulls event from an event queue, that most likely runs in a separate thread (maybe windows iostream?). This is implemented in the matlab gui elements in the form of callback functions. You do not need to think about it yourself, since the callback functions are already defined. What you must do however is to define an action that will occur when the callback is triggered. This is done in this simple example.
First write a function myCallback that will change color of the plotted line when triggered:
function myCallback(hObject,eventdata)
c = 'krym';
n = randi([1,4],1);
ch = get(gca,'Children');
set(ch,'Color',c(n));
Then create a plot and tell the gui to perform myCallback when triggered:
plot(1:10,'LineW',2)
set(gcf,'ButtonDownFcn',@myCallback)
This will change line color of the plotted line when I click the mouse on the figure, but outside the axes. You may instead want to write a function calling close when some action is triggered and break then break the loop if the figure is closed.
댓글 수: 0
추가 답변 (2개)
Robert Cumming
2014년 7월 28일
You can do it all in your loop:
h = figure;
ax = axes( 'parent', h, 'nextplot', 'add', 'xlim', [0 200] );
x = [1:2000];
y = nan(1,2000);
for i=1:2000;
drawnow()
if ~ishandle ( h ); break; end
y(i) = rand(1);
plot ( x, y, 'rs-' );
end
fprintf ( 'user exited after %i iterations\n', i );
% continue your code
Obviously this deletes your plot after the loop - but the idea is that you have a figure (could be a msgbox...) which the user can use to interrupt the process. The for loop breaks when this figure handle is no longer valid (i.e. its been deleted).
댓글 수: 1
Patrik Ek
2014년 7월 28일
Great solution. The problem is only that the code becomes less flexible than if an own callback function is generated. For example it would be possible to save the figure before closing in a callback. It would also be possible to keep the figure using a global variable exitThisForLoop (I know globals are in general bad, but in this case it is far simpler than any other solution). However, this solution is really simple and that is always good.
Julia
2014년 7월 28일
See "break" and "return" in the Matlab help.
댓글 수: 2
David Young
2014년 7월 28일
Isn't the essence of the question about how to manage the user interaction?
Patrik Ek
2014년 7월 28일
I think so too, and return returns from the function, which is not what is expected.
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Object Programming에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!