Stop running program without exiting current GUI

  • Follow


I am currently writting a program with GUI. In the GUI, there is a main program. When I run the main program and it takes a bit time to finish. Sometimes I need stop the program and re-run a new one during the old program running, the only way I can do,by far, is to press 'ctl + c' to kill the program,then restart a new one. Is any other way I can do without exiting current GUI  and without using command window (also the GUI keeps the current state when I run a new program)?

Thanks
0
Reply yan 12/14/2010 9:20:05 AM

"yan huang" <yan.huang1@unsw.edu.au> wrote in message <ie7cs4$j5k$1@fred.mathworks.com>...
> I am currently writting a program with GUI. In the GUI, there is a main program. When I run the main program and it takes a bit time to finish. Sometimes I need stop the program and re-run a new one during the old program running, the only way I can do,by far, is to press 'ctl + c' to kill the program,then restart a new one. Is any other way I can do without exiting current GUI  and without using command window (also the GUI keeps the current state when I run a new program)?
> 
> Thanks

Hi,

I assume that you want to cancel a &#8220;for&#8221; loop in your &#8220;main&#8221; program in your GUI.

Then just add a push button into your GUI and whenever you press the push button, change its &#8220;userdata&#8221; properties to 1. And add following code into your for loop:

%-------CANCELLATION OF THE LOOP---------% 
if get(handles.yourPushButtonTag, 'userdata') == 1
    %	Set the cancel pushbutton user data to default
    set(handles.yourPushButtonTag, 'userdata', 0);
    
    % Update handles structure if you want to update the data in your Gui
    % guidata(hObject, handles);
    return;            
end

Regards,
0
Reply John 12/14/2010 10:38:04 AM


On 14/12/10 3:20 AM, yan huang wrote:
> I am currently writting a program with GUI. In the GUI, there is a main
> program. When I run the main program and it takes a bit time to finish.
> Sometimes I need stop the program and re-run a new one during the old
> program running, the only way I can do,by far, is to press 'ctl + c' to
> kill the program,then restart a new one. Is any other way I can do
> without exiting current GUI and without using command window (also the
> GUI keeps the current state when I run a new program)?

Upon detecting (by some way *other* than control-c) that you want to 
interrupt, have a callback open a GUI window that asks what should be 
done and execute the second program from within there. When you are 
finished with that, exiting the callback would resume execution of the 
original program. Provided, of course, that you haven't clear'd its 
variables.
0
Reply Walter 12/14/2010 3:34:53 PM

"John " <onsekizdegilim@yahoo.co.uk> wrote in message <ie7hec$4nj$1@fred.mathworks.com>...
> "yan huang" <yan.huang1@unsw.edu.au> wrote in message <ie7cs4$j5k$1@fred.mathworks.com>...
> > I am currently writting a program with GUI. In the GUI, there is a main program. When I run the main program and it takes a bit time to finish. Sometimes I need stop the program and re-run a new one during the old program running, the only way I can do,by far, is to press 'ctl + c' to kill the program,then restart a new one. Is any other way I can do without exiting current GUI  and without using command window (also the GUI keeps the current state when I run a new program)?
> > 
> > Thanks
> 
> Hi,
> 
> I assume that you want to cancel a &#8220;for&#8221; loop in your &#8220;main&#8221; program in your GUI.
> 
> Then just add a push button into your GUI and whenever you press the push button, change its &#8220;userdata&#8221; properties to 1. And add following code into your for loop:
> 
> %-------CANCELLATION OF THE LOOP---------% 
> if get(handles.yourPushButtonTag, 'userdata') == 1
>     %	Set the cancel pushbutton user data to default
>     set(handles.yourPushButtonTag, 'userdata', 0);
>     
>     % Update handles structure if you want to update the data in your Gui
>     % guidata(hObject, handles);
>     return;            
> end
> 
> Regards
For your method, I do following
1) set the 'userdata' value '1' in callback function
function pushbutton_Stop_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_Stop (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.pushbutton_Stop, 'userdata',1)

2)then write some codes in pushbutton_Run Callback function
function pushbutton_Run_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_Run (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);
global num1 num2
num1=str2num(get(handles.edit1,'String'));
num2=str2num(get(handles.edit2,'String'));
count=0;
while 1
    %-------CANCELLATION OF THE LOOP---------% 
    if get(handles.pushbutton_Stop, 'userdata') == 1
        % Set the cancel pushbutton user data to default
        set(handles.pushbutton_Stop, 'userdata', 0);

         % Update handles structure if you want to update the data in your Gui
        % guidata(hObject, handles);
        return; 
    end
    total=num1+num2
    
    count=count+1
end

when I click 'Run' button, it go infinite loop, But when I click 'Stop' button, the program still run in the infinite loop. It is supposed to stop program. What did I do wrong?
0
Reply yan 12/14/2010 4:43:07 PM

"yan huang" <yan.huang1@unsw.edu.au> wrote in message <ie86qr$9dk$1@fred.mathworks.com>...
> "John " <onsekizdegilim@yahoo.co.uk> wrote in message <ie7hec$4nj$1@fred.mathworks.com>...
> > "yan huang" <yan.huang1@unsw.edu.au> wrote in message <ie7cs4$j5k$1@fred.mathworks.com>...
> > > I am currently writting a program with GUI. In the GUI, there is a main program. When I run the main program and it takes a bit time to finish. Sometimes I need stop the program and re-run a new one during the old program running, the only way I can do,by far, is to press 'ctl + c' to kill the program,then restart a new one. Is any other way I can do without exiting current GUI  and without using command window (also the GUI keeps the current state when I run a new program)?
> > > 
> > > Thanks
> > 
> > Hi,
> > 
> > I assume that you want to cancel a &#8220;for&#8221; loop in your &#8220;main&#8221; program in your GUI.
> > 
> > Then just add a push button into your GUI and whenever you press the push button, change its &#8220;userdata&#8221; properties to 1. And add following code into your for loop:
> > 
> > %-------CANCELLATION OF THE LOOP---------% 
> > if get(handles.yourPushButtonTag, 'userdata') == 1
> >     %	Set the cancel pushbutton user data to default
> >     set(handles.yourPushButtonTag, 'userdata', 0);
> >     
> >     % Update handles structure if you want to update the data in your Gui
> >     % guidata(hObject, handles);
> >     return;            
> > end
> > 
> > Regards
> For your method, I do following
> 1) set the 'userdata' value '1' in callback function
> function pushbutton_Stop_Callback(hObject, eventdata, handles)
> % hObject    handle to pushbutton_Stop (see GCBO)
> % eventdata  reserved - to be defined in a future version of MATLAB
> % handles    structure with handles and user data (see GUIDATA)
> set(handles.pushbutton_Stop, 'userdata',1)
> 
> 2)then write some codes in pushbutton_Run Callback function
> function pushbutton_Run_Callback(hObject, eventdata, handles)
> % hObject    handle to pushbutton_Run (see GCBO)
> % eventdata  reserved - to be defined in a future version of MATLAB
> % handles    structure with handles and user data (see GUIDATA)
> handles.output = hObject;
> 
> % Update handles structure
> guidata(hObject, handles);
> global num1 num2
> num1=str2num(get(handles.edit1,'String'));
> num2=str2num(get(handles.edit2,'String'));
> count=0;
> while 1
>     %-------CANCELLATION OF THE LOOP---------% 
>     if get(handles.pushbutton_Stop, 'userdata') == 1
>         % Set the cancel pushbutton user data to default
>         set(handles.pushbutton_Stop, 'userdata', 0);
> 
>          % Update handles structure if you want to update the data in your Gui
>         % guidata(hObject, handles);
>         return; 
>     end
>     total=num1+num2
>     
>     count=count+1
> end
> 
> when I click 'Run' button, it go infinite loop, But when I click 'Stop' button, the program still run in the infinite loop. It is supposed to stop program. What did I do wrong?

Forgot to say that you need add a pause in your loop too.

while 1
    %-------CANCELLATION OF THE LOOP---------%
    if get(handles.pushbutton_Stop, 'userdata') == 1
        % Set the cancel pushbutton user data to default
        set(handles.pushbutton_Stop, 'userdata', 0);

         % Update handles structure if you want to update the data in your Gui
        % guidata(hObject, handles);
        return;
    end
    total=num1+num2
    
    count=count+1

    pause(0.01); %	this line will add 0.01 second delay
end


If you want to execute another program after you stop the loop, call the function before "return".

Regards.
0
Reply John 12/14/2010 10:03:05 PM

"yan huang" wrote in message <ie7cs4$j5k$1@fred.mathworks.com>...
> I am currently writting a program with GUI. In the GUI, there is a main program. When I run the main program and it takes a bit time to finish. Sometimes I need stop the program and re-run a new one during the old program running, the only way I can do,by far, is to press 'ctl + c' to kill the program,then restart a new one. Is any other way I can do without exiting current GUI  and without using command window (also the GUI keeps the current state when I run a new program)?
> 
> Thanks

Check this link (it is not mine, full credit goes to the author) http://blinkdagger.com/matlab/matlab-gui-tutorial-how-to-stop-a-long-running-function/comment-page-1/#comment-7417 . I had the same problem and this solved it perfectly ;) .
0
Reply amt015 (5) 3/31/2011 8:35:05 AM

5 Replies
524 Views

(page loaded in 0.24 seconds)

Similiar Articles:













7/25/2012 9:49:10 PM


Reply: