Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Stop a running program from a GUI (equivalent of ctrl C)?

Subject: Stop a running program from a GUI (equivalent of ctrl C)?

From: Camille Couzi

Date: 21 Sep, 2011 05:30:30

Message: 1 of 8

Hi there
is there a function or command I can write as a callback of a "Stop Process" push button so that it stops the running program FROM A GUI?
(I was looking at break help, but it is just to stop for and while loops, I would like something more general...)

Thanks in advance for your help!!

Subject: Stop a running program from a GUI (equivalent of ctrl C)?

From: Bruno Luong

Date: 21 Sep, 2011 06:31:13

Message: 2 of 8

"Camille Couzi" <cco@dhi.com.sg> wrote in message <j5bspm$ng2$1@newscl01ah.mathworks.com>...
> Hi there
> is there a function or command I can write as a callback of a "Stop Process" push button so that it stops the running program FROM A GUI?
> (I was looking at break help, but it is just to stop for and while loops, I would like something more general...)
>
> Thanks in advance for your help!!

One idea is to wrap your GUI in a try/catch.

try
  lauchmyGUI()
catch
  fprintf('exit')
end

Then everywhere where a (simulated) error is triggered,
  error('dummy')

it will end up in the above catch statement.

Bruno

Subject: Stop a running program from a GUI (equivalent of ctrl C)?

From: Camille Couzi

Date: 21 Sep, 2011 07:02:14

Message: 3 of 8

Ey bruno
thanks for your quick reply!
I have never used the try/catch function. when you write:
try
    lauchmyGUI()
catch
    fprintf('exit')
end

Do you mean that I should put my launch function between "try" and "catch"?
where do I write error('dummy')? inside my launch function?

thanks again for the help...
camille

Subject: Stop a running program from a GUI (equivalent of ctrl C)?

From: Bruno Luong

Date: 21 Sep, 2011 07:18:14

Message: 4 of 8


> Do you mean that I should put my launch function between "try" and "catch"?

Yes

> where do I write error('dummy')? inside my launch function?
>

Where you want to break, not necessary inside the main function, buried anywhere should also work.

Bruno

Subject: Stop a running program from a GUI (equivalent of ctrl C)?

From: Camille Couzi

Date: 21 Sep, 2011 07:30:31

Message: 5 of 8

Ok, the thing is that I am making a GUI: mysript1.m contains the figure and buttons and callbacks. Within the callback function of the "Ok Launch" button I call the function which is in myscript2.m and make all the processings for all the files included in a folder
myscript2(input1,input2... )

 (so i call this function in a for loop). myscript2 makes all the calulations.

I want to include a button in my GUI "stop the processing", so that when the user press it, the function stop make the calulations (but the GUI doens't close).

I have read many posts in the forum but Ican't find out a simple way to make that...
 so following your advice, I should write in my "ok launch" callback:
try
    myscript2(input1,input2... )
catch
    fprintf('exit')
end

and in the callback of "stop the processing" button I write
error('dummy')
??

What if the user press "stop" button when matlab has already entered inside the processing function (I say that because my function is quite long, as it processes huge files). then it will only stop the processing when entering the for loop ffor the next file right?

thanks again for the help :-)

Subject: Stop a running program from a GUI (equivalent of ctrl C)?

From: Nasser M. Abbasi

Date: 21 Sep, 2011 07:38:29

Message: 6 of 8

On 9/21/2011 12:30 AM, Camille Couzi wrote:
>
> I want to include a button in my GUI "stop the processing", so that when the user press
>it, the function stop make the calulations (but the GUI doens't close).

> thanks again for the help :-)

may be this helps:

have a button 'stop', in its callback, set a flag to true.

in the main loop (the other callback which busy in a loop),
it will check this flag at the top (or bottom) of its loop.

If the flag is set true, it makes it false right away and
terminate the loop and returns.

So, the most the user has to wait after hitting STOP is for one
iteration to complete if the hit happened just after the last check.

callbacks interrupt each others, so the above works.

The flag is set in userData buffer and read from there
by the other callback each time.

--Nasser

Subject: Stop a running program from a GUI (equivalent of ctrl C)?

From: Camille Couzi

Date: 21 Sep, 2011 08:45:30

Message: 7 of 8

Thanks Nasser,
This seem to be the easiest solution.

camille.

Subject: Stop a running program from a GUI (equivalent of ctrl C)?

From: Nasser M. Abbasi

Date: 21 Sep, 2011 09:16:44

Message: 8 of 8

On 9/21/2011 1:45 AM, Camille Couzi wrote:
> Thanks Nasser,
> This seem to be the easiest solution.
>
> camille.

you are welcom. The above is how I do it in all my Matlab
GUI applications. This snippets code from one example

working callback (solving an ODE, output function), the 'process'
-------------------------
  function status= output(t,x,~,data)
         %called by ode45 after each step. Plot the current
         %pendulum position for simulation
         
         userData = get(data.handles.figure1, 'UserData');
         if userData.stop
             status=true;
             g_status =true;
         else
             status = false;
             .... do real work
         end
     end
------------------

when user hits the 'run' button. The callback:

-------------------------------
% --- Executes on button press in run_btn.
function run_btn_Callback(hObject, eventdata, handles)
% hObject handle to run_btn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

[data,status] = parse_input(handles);
if not(status)
     return;
end
.....
userData.stop = false;
set(handles.figure1, 'UserData',userData);

...
--------------------------------------------------

stop button callback

----------------------------
% --- Executes on button press in reset_tag.
function reset_tag_Callback(hObject, eventdata, handles)
% hObject handle to reset_tag (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)


data = get(handles.figure1, 'UserData');
data.stop = true;
set(handles.figure1, 'UserData',data);
.....
--------------------------

--Nasser

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us