Stop permanently execution of codes under a Callback using another Callback
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi there.. I have a GUI with a 'Compute' pushbutton and a 'Stop Calculation' pushbutton. I would like to know how can I stop (not interrupting temporarily) the execution of codes which come under 'Compute' button immediately after clicking 'Stop Calculation' button without completing the remaining codes. I am attaching the demo of codes associated with the 'Compute' button here. What will be the additional codes I have to add to the 'Compute' as well as 'Stop Calculation' button in order to achieve this task? Looking forward for a help on this. Thanks in advance!
function pushbutton3_Callback(hObject, eventdata, handles) %Compute
%---------------If Auto run mode is on-------------------
if(get(handles.radiobutton1,'Value')==1)
% certain lines of code
tstart=tic; tstop=0;
if(isnan(a))
a=inf;
end
while(((~(get(handles.radiobutton1,'Value')))||tstop<a*60) && getappdata(handles.pushbutton13,'Value')==1)
h = waitbar(0,'computing');
if (get(handles.radiobutton8,'Value')==1)
% certain lines of code
end
waitbar(0.1,h,'computing');
if (get(handles.radiobutton9,'Value')==1)
% certain lines of code
end
waitbar(0.2,h,'computing');
if (get(handles.radiobutton10,'Value')==1)
% certain lines of code
end
waitbar(0.3,h,'computing');
if(get(handles.radiobutton3,'Value')==1)
% certain lines of code
end
waitbar(0.5,h,'computing');
if(get(handles.radiobutton4,'Value')==1)
% certain lines of code
end
waitbar(0.7,h,'computing');
if(get(handles.radiobutton5,'Value')==1)
% certain lines of code
end
waitbar(0.9,h,'computing');
if(get(handles.radiobutton6,'Value')==1)
% certain lines of code
end
waitbar(1,h,'computing');
end
else
%---------------If Autorun mode is not on--------------
h = waitbar(0,'computing');
if (get(handles.radiobutton8,'Value')==1)
% certain lines of code
end
waitbar(0.1,h,'computing');
if (get(handles.radiobutton9,'Value')==1)
% certain lines of code
end
waitbar(0.2,h,'computing');
if (get(handles.radiobutton10,'Value')==1)
% certain lines of code
end
waitbar(0.3,h,'computing');
if(get(handles.radiobutton3,'Value')==1)
% certain lines of code
end
waitbar(0.5,h,'computing');
if(get(handles.radiobutton4,'Value')==1)
% certain lines of code
end
waitbar(0.7,h,'computing');
if(get(handles.radiobutton5,'Value')==1)
% certain lines of code
end
waitbar(0.9,h,'computing');
if(get(handles.radiobutton6,'Value')==1)
% certain lines of code
end
waitbar(1,h,'computing');
close(h)
h = msgbox('Computation Done');
end
댓글 수: 0
채택된 답변
Andrew Newell
2015년 3월 9일
Reena, have a look at How can I interrupt a callback and NOT come back to finish its execution in MATLAB?
댓글 수: 0
추가 답변 (1개)
Eric
2017년 11월 29일
I was able to work cause a slider control to stop executing with a pushbutton which changes a variable. The variable value then causes another portion of a if statement to execute which does not include the slider. Code below the slide can then execute without the slider serving any further purpose. I am sharing because I found that this is simpler than some other solution out there although it's still probably not that elegant. Please note that this was done through a LOT of trial and error, so some of the code may be unnecessary or redundant (I didn't have time to clean it up yet).
function plotcallback
imagenum=1;
f3=figure('units','normalized','outerposition',[0 0 1 1])
xscaleforNonTrimedgrayImage=1:100
yscaleforNonTrimedgrayImagebutrescaled=1:100
NonTrimedgrayImagebutrescaled=rand(100,100,100);
global f3
im1=surf(xscaleforNonTrimedgrayImage,yscaleforNonTrimedgrayImagebutrescaled,NonTrimedgrayImagebutrescaled(:,:,imagenum),'EdgeColor','none')
set(im1,'linestyle','none')
grid off
view(0,90)
xlabel('length(mm)','fontsize',20);ylabel('length(mm)','fontsize',20)
ca3=gca;
set(ca3,'fontsize',16)
axis equal tight
shading flat
view(2)
cmap = contrast(NonTrimedgrayImagebutrescaled(:,:,1) )
colormap('Jet')
caxis( [min(min(NonTrimedgrayImagebutrescaled(:,:,imagenum))) max(max(NonTrimedgrayImagebutrescaled(:,:,1)))]);
cb=colorbar('YLim',[min(min(NonTrimedgrayImagebutrescaled(:,:,imagenum))) max(max(NonTrimedgrayImagebutrescaled(:,:,1)))]);
set(gca,'CLim',[min(min(NonTrimedgrayImagebutrescaled(:,:,imagenum))) max(max(NonTrimedgrayImagebutrescaled(:,:,imagenum)))]);
zlim auto
%%correct code for slider
hold on;
H = uicontrol('Style','slider','Min',1,'Max',100,...
'SliderStep',[1/100 (1/100)],'Value',1,...
'Position',[20 20 300 20],'callback',@sliderCallback);
anno_obj_handl=annotation('textbox','String','The image number is 1','FitBoxToText','on','BackgroundColor','white');
stopnow=1
btn = uicontrol('Style', 'pushbutton', 'String', 'Stop scrolling through images',...
'Position', [20 40 250 20]);
set(btn,'Callback',@(hObject,eventdata) stopnow==2)% & ylabel('length(mm)','fontsize',20) & view(0,90));
% short pause so that this loop can be interrupted
pause(0.01);
function sliderCallback(~,~)
if stopnow==1,
reset(im1)
%%removed code (testing only)
%reset(anno_obj_handl)
%clear all except H F lilh
%delete(findall(f3,'type','annotation'))
delete(anno_obj_handl)
A=round(get(H,'value'))
im1=surf(xscaleforNonTrimedgrayImage,yscaleforNonTrimedgrayImagebutrescaled,NonTrimedgrayImagebutrescaled(:,:,A),'EdgeColor','none')
str=sprintf('The image number is %d',A)
anno_obj_handl=annotation('textbox','String',str,'FitBoxToText','on','BackgroundColor','white');
else
delete
end
end
k = waitforbuttonpress
[x,y] = ginput(2)
rangex=abs(x(1)-x(2))
rangey=abs(y(1)-y(2));
f = warndlg( 'close window to complete (you may leave open to check variables)','excution has puased.');
drawnow % Necessary to print the message
waitfor(f);
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!