How to use pushbutton to stop a loop?

조회 수: 6 (최근 30일)
adi kul
adi kul 2016년 7월 21일
댓글: adi kul 2016년 7월 21일
Hello All, I am working on the GUI of my code. I have start code which works in for loop. More the user inputs bigger loops. So I want to have a stop button which will close the process at the loop when it is pressed. So far I tried this with no luck:
For Stop button I have kept this:
function stop_Callback(hObject, eventdata, handles)
% hObject handle to stop (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
I1.kill=1;
set(handles.stop, 'UserData', I1);
Now in my start button call back I have this:
I1= get(handles.stop, 'UserData');
And within the for loop I have added this:
for ii = 1:length(fVal)
if I1.kill~=1
I know this is not correct and that's why I am getting:
Attempt to reference field of non-structure array.
But I would like to know a work around to make stop button.

채택된 답변

Geoff Hayes
Geoff Hayes 2016년 7월 21일
adi - the error message is probably being generated because you are trying to access a field of a structure that has not yet been defined (since the invoking the stop button occurs after pressing start). Another problem with the above is that in the start button callback, you call
I1= get(handles.stop, 'UserData');
outside of the for loop and so I1 would be static for the remainder of the iterations. You would need to continually update this local variable on each iteration of the for loop to see if there has been a change.
An alternative to the above is to just use the handles structure. In your stop button callback do
function stop_Callback(hObject, eventdata, handles)
handles.endLoop = true;
guidata(hObject, handles);
Now, in your start button callback, you would do
function start_Callback(hObject, eventdata, handles)
% do stuff
for ii = 1:length(fVal)
% get the updated GUI handles
guiHandles = guidata(hObject);
if ~isempty(guiHandles) && isfield(guiHandles,'endLoop') && guiHandles.endLoop == 1
guiHandles.endLoop = 0;
guidata(hObject,guiHandles);
% exit the loop
break;
end
% do other stuff
% pause the loop to allow other callbacks to fire
pause(0.01);
end
The pause is important because without it, the above for loop is "tight" and won't allow any interruption...so you could press the stop button as many times as you want but there would be no effect until the loop had finished.
So try updating the handles structure instead, get the latest on each iteration of your for loop, and do the pause to allow the stop callback to fire.
  댓글 수: 2
adi kul
adi kul 2016년 7월 21일
Hey thanks Geoff, I tried this and I am getting this error:
Error using guidata (line 89)
H must be the handle to a figure or figure descendent.
on line:
guiHandles = guidata(hObject);
adi kul
adi kul 2016년 7월 21일
no issues. did a re start and it worked flawlessly!! Thank you Geoff

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by