How to check whether button is pressed

Hello, I have an idea to simulate scope output in my gui. My plan was to get block parameters, value and time, and then use drawnow to keep updating the plot. Everything was fine, but I had to use infinite while loop, cause my simulation stops as user clicks certain button. That's the problem - as program goes into that loop it won't execute any other instruction, so it can't chcek whether "Off" button is pressed (at least I think that's the way it works, because that button should stop the simulation as well, but it doesn't...) . Is there any way to realize that idea?
Here's my code:
block = [...]
rto = get_param(block,'RuntimeObject');
i=1;
while 1 == 1
val(i) = rto.OutputPort(1).Data;
time(i) = rto.CurrentTime;
pause(0.01)
drawnow
plot(time,val)
i = i+1;
end

12 Comments

Have you tried putting a break statement inside the while loop? Something like this:
block = [...]
rto = get_param(block,'RuntimeObject');
i=1;
while true
val(i) = rto.OutputPort(1).Data;
time(i) = rto.CurrentTime;
pause(0.01)
drawnow
plot(time,val)
if ... (if "Off" button is pressed - you fill in the logic)
break
end
i = i+1;
end
Thank you for you response. That's what I've been thinking of, but I don't know how to declare whether button is pressed within function of another button... Do you know the solution for that?
I'm not sure how running in Simulink affects things, but typically in a GUI you have handles to all your objects (buttons, text boxes, axes, etc.), and so you just have to check the state of the button, e.g.:
if app.YourButton.Value
break
end
or
if get(handles.YourButton,'Value')
break
end
or similar, depending on the type of button (uicontrol or not) and the ui(figure) development environment (AppDesigner/GUIDE/none).
Ok, I tried this, but it still doesn't seem to work. I smash that second button, but the loop continues anyway. I don't think it's Simulnik that affect all things, but it's that loop. As program still executes instructions from first button it simply doesn't check everything else...
The drawnow should ensure that MATLAB is using the latest button state, so the loop should exit when the button is down.
Run this simple example from the command line, and see if the loop stops when you click the button:
f = figure();
btn = uicontrol('Parent',f,'Style','togglebutton','String','Stop','Value',0);
while true
if get(btn,'Value')
disp('Stop button down: exiting the loop!')
break
end
drawnow
end
If it works, then Simulink or something else is preventing the same construction from working in your code.
Your example indeed works, but when I apply such solution into my code it doesn't. Maybe it's the nature of the button...
(I use function button_Callback(hObject, eventdata, handles))
Thank you for your help, I'll try to get it to work using uicontrol...
Okay, I got it simpler way. I changed property of the button from pushbutton to togglebutton and now it works! Thank you once again
P.S
There's no need for drawnow in that solution
Here are a couple of functional demos on this topic
Is your loop a nested loop? If so, break will only exit from the loop in which it occurs.
Are you resetting the button state after the button is pressed?
If a drawnow solves the problem, do let us know because that would be unexpected.
The while loop in the original question does not contain a condition similar to Voss's comment. To help further, we'll need to see your while loop.
Voss
Voss on 25 Jan 2024
Edited: Voss on 25 Jan 2024
"I changed property of the button from pushbutton to togglebutton and now it works! Thank you once again"
Great!
"There's no need for drawnow in that solution"
When I run this:
f = figure();
btn = uicontrol('Parent',f,'Style','togglebutton','String','Stop','Value',0);
while true
if get(btn,'Value')
disp('Stop button down: exiting the loop!')
break
end
% drawnow
end
The loop continues running after I click the button until I do ctrl+c.
Oh, my mistake, I should have point out that I was referring to mine solution with togglebutton instead of pushbutton. For your code it is necessary.
Btw. funny coincidence, as I'm already using drawnow to update the plot in my gui...but I check and even without it, second button (togglebutton) can break the loop triggered by first button.

Sign in to comment.

Answers (0)

Categories

Products

Release

R2016b

Asked:

on 25 Jan 2024

Commented:

on 25 Jan 2024

Community Treasure Hunt

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

Start Hunting!