How to check whether button is pressed

26 views (last 30 days)
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
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
Lukasz Grzybowski on 25 Jan 2024
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

Find more on Simulink Environment Customization in Help Center and File Exchange

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!