How to check whether button is pressed
Show older comments
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
Voss
on 25 Jan 2024
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
Lukasz Grzybowski
on 25 Jan 2024
Voss
on 25 Jan 2024
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).
Lukasz Grzybowski
on 25 Jan 2024
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.
Lukasz Grzybowski
on 25 Jan 2024
Lukasz Grzybowski
on 25 Jan 2024
Lukasz Grzybowski
on 25 Jan 2024
Adam Danz
on 25 Jan 2024
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
on 25 Jan 2024
"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.
Lukasz Grzybowski
on 25 Jan 2024
Answers (0)
Categories
Find more on Simulink Environment Customization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!