Is there a possibility to make a Start/Stop Pushbutton in guide?

Hey there, I am having a Matlab Gui designed by guide and want to Start/Stop a measurement with one button. Is there a possibility to just stop the measurement by using the same pushbutton and jump out of the function? Or do I have to use two pushbuttons?

1 Commento

You can have the pushbutton callback function do anything you like, so there is no need to have two buttons. Of course assuming your measurements are interruptible.

Accedi per commentare.

 Risposta accettata

Stephen23
Stephen23 il 18 Giu 2018
Modificato: Stephen23 il 18 Giu 2018
"Is there a possibility to just stop the measurement by using the same pushbutton and jump out of the function"
Of course you could use a push button to not a logical scalar (or and, or whatever logic you want), or as an alternative you could use a toggle button.

4 Commenti

Hey Stephen, I dont't actually get what you mean. I tried to assign a variable, that is 1 at Start and 0 at Stop. But somehow the variable don't update and when I press it again it always jumps into the 'Start-Function'.
You need to store or pass your variable.
e.g.:
function myfunction(hObj,event)
flag=getappdata(hObj,'flag');
if isempty(flag)==1 || flag==0
%start
flag=1;
else
%stop
flag=0
end
setappdata(hObj,'flag',flag)
end
I think it might be better to actually set flag to 0 after you created your button and omit the isempty.
setappdata(yourbuttonhandle,'flag',0)
As alternative you could use a property of your button:
function myfunction(hObj,~)
string = get(hObj,'String'); %needs to be 'Start' when first time executed
if strcmp(string,'Start')
%start
set(hObj,'string','Stop')
else
%stop
set(hObj,'String','Start')
end
end
Thanks. The last suggestion from you worked well. But after the 'Stop' function ran, the function jumps back to the line where I pressed the Stop button. The behaviour is also described at:
https://de.mathworks.com/help/matlab/creating_guis/callback-sequencing-and-interruption.html
"then MATLAB stops the execution of the running callback and executes the interrupting callback. MATLAB resumes executing the running callback when the interrupting callback completes." Is there a way to not jump back into the 'Start' function?
"Is there a way to not jump back into the 'Start' function?"
Not really, because that is the exact concept of interrupts. But you can easily make the Start function check the logical value, or the value of a toggle button. Have a look at the 'Demo' button of my FEX submission cubehelix_view, which lets the while loop iterate indefinitely until the user clicks the button again.

Accedi per commentare.

Più risposte (0)

Richiesto:

il 18 Giu 2018

Commentato:

il 19 Giu 2018

Community Treasure Hunt

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

Start Hunting!

Translated by