Clear Filters
Clear Filters

uicontrols, trying to add a state button to a figure not a uifigure

6 views (last 30 days)
I currently have a figure created with a plot, menu, and a few push buttons. It was not created, however, with the uifigure function and I don't want to make it into a uifigure. I'm now trying to add a start stop button to this figure to later use with a switch statement.
Ideally, I want to add a state button as it's value is easy to interpret. However, it currently looks like state buttons are only available for use on uifigures but please let me know if I'm wrong!
I also tried a toggle button which does appear on my figure but I can't figure out how to tell if it's been pushed or not as it's my only button and not a part of a button group.
I'm not including code as it's really long and I'm asking for a general answer, not super specific to my scenario.

Answers (1)

Walter Roberson
Walter Roberson on 3 Mar 2023
For traditional figures, use
startstop = uicontrol('style', 'toggle', ...
'String', 'Start', ...
'Position', as_appropriate);
and save a reference to that somewhere you can get to it
Now at any point you can check the Value of that control. 1 indicates that the control is "on", 0 indicates that it is off.
toggle controls stay on (or off) until they are changed, so as long as your code checks the button state faster than the user clicks the button again, you will be able to tell what the state of the button is.
When you first detect that it is on, you will probably want to change the String to 'Stop', and when you detect that they want to stop, you would change the string back to 'Start'.
You can consider setting a Callback property so that you do not need to loop actively checking whether the user has asked to start yet. The callback would have to start the computation. But watch out for the fact that the callback would still be active and responsive when the user clicks to stop -- you might want to remove the callback property while the callback is executing the work.
You could also consider using uicontrol('style', 'push') . If you use that, then beware that the Value of the control will only be 1 in the time between whent the user pushes the button and the time the activated callback returns -- in particular if there is no Callback property configured for uicontrol 'push' then the Value will be 1 only for a brief time -- pretty much only detectable in the case where you are already executing a callback on a button that has been marked to 'queue' additional callbacks (and I'm not sure even then... it might potentially never set the Value property at all if no Callback is configured.) So if you want to use 'push' you pretty much need to use Callback -- though the entire action of the callback might be to set a 'stop requested' flag inside your code.

Categories

Find more on Develop uifigure-Based Apps in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!