- What happens if the mainButton is pressed before one of the option buttons are pressed? You'll need to set up the UserData in mainButton with a default value when the GUI is created.
- Will there be any visual indication which button was most recently pressed? You could change the color of the mainButton or have some text near the button...
- Instead of using 4 option buttons, consider using a popup menu or some other GUI component that is designed for such a task (again, I'm giving you the benefit of doubt that this setup makes sense for your project but I'm skeptical).
How to make a button works depending on which button a selected before?
12 views (last 30 days)
Show older comments
In my GUI, first there are four options of button, depending on which is chosen i want that another button (when is selected) run certain things than will be shown in some text boxes. (all buttons are pushbuttons)
0 Comments
Accepted Answer
Adam Danz
on 21 Sep 2018
Edited: Adam Danz
on 21 Sep 2018
This sounds like a potential mess but I'll give it the benefit of doubt and suggest a solution. But please make sure your idea makes sense and that a naive user will understand how to use the GUI.
Let's say your 4 option buttons are b1, b2, b3, b4 and your text button is mainButton. One of the properties of mainButton (and any button) is UserData and you can store anything you want in there and reference it later when needed.
Within each of the option button callback functions, store information about which button was just pressed in the UserData field of the mainButton handle. That would look something like this:
function b1CallbackFunction(hObject, eventdata, handles)
handles.mainButton.UserData = 'b1';
end
You'll do that for each option button.
Then in the callback function of mainButton you'll need to access the UserData and create a switch-case that controls the different actions take for each option button.
That would look something like this
function mainButtonCallbackFunction(hObject, eventdata, handles)
mostRecentButton = hObject.UserData;
switch mostRecentButton
case 'b1'
% do something
case 'b2'
% do something
case 'b3'
% do something
case 'b4'
%do something
otherwise
error('Unexpected button handle')
end
A couple of important things to think about:
All of this code was written 'on the fly' and not tested.
15 Comments
More Answers (1)
Andrea Salazar
on 3 Oct 2018
Edited: Andrea Salazar
on 3 Oct 2018
2 Comments
Adam Danz
on 3 Oct 2018
Good job! I still think you should use the cleaner version with less redundancies. All you need to do is make sure all of the " handle " typos are " handles ". I've edited that in my comment above.
See Also
Categories
Find more on Annotations 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!