How to check whether button is pressed

18 visualizzazioni (ultimi 30 giorni)
Lukasz Grzybowski
Lukasz Grzybowski il 25 Gen 2024
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 Commenti
Voss
Voss il 25 Gen 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 il 25 Gen 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.

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Simulink Environment Customization in Help Center e File Exchange

Prodotti


Release

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by