How to make a button works depending on which button a selected before?

12 views (last 30 days)
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)

Accepted Answer

Adam Danz
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:
  • 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).
All of this code was written 'on the fly' and not tested.
  15 Comments
Andrea Salazar
Andrea Salazar on 3 Oct 2018
Edited: Andrea Salazar on 3 Oct 2018
I changed text100 for text45
The class handle has no property or method named 'text45'.
Error in intento1000>setStrings (line 632) set(handle.text45,'String',values(1,1));
Error in intento1000>boton_ok_Callback (line 669) setStrings(P_G);
Error in gui_mainfcn (line 95) feval(varargin{:});
Error in intento1000 (line 42) gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)intento1000('boton_ok_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
Andrea Salazar
Andrea Salazar on 3 Oct 2018
I think one of the mistakes is "handle" instead of "handles".. but still there is the same error

Sign in to comment.

More Answers (1)

Andrea Salazar
Andrea Salazar on 3 Oct 2018
Edited: Andrea Salazar on 3 Oct 2018
The error persists.. :(.. i changed the tag, erase the textbox and changed tag, but still does not work, now nothing work, not even G
function boton_ok_Callback(hObject, eventdata, handles
mostRecentButton=hObject.UserData;
switch mostRecentButton
case 'b1'
A_1=str2double(get(handles.G_A,'String'));
B_1=str2double(get(handles.G_B,'String'));
C_1=str2double(get(handles.G_C,'String'));
D_1=str2double(get(handles.G_t,'String'));
E_1=str2double(get(handles.G_R,'String'));
[P_G]=G(A_1,B_1,C_1,D_1,E_1);
setStrings(P_G);
case 'b2'
A_2=str2double(get(handles.C_A,'String'));
B_2=str2double(get(handles.C_B,'String'));
C_2=str2double(get(handles.C_t,'String'));
D_2=str2double(get(handles.C_R,'String'));
[P_C]=C(A_2,B_2,C_2,D_2);
setStrings(P_C);
case 'b3'
A_3=str2double(get(handles.O_A,'String'));
B_3=str2double(get(handles.O_B,'String'));
C_3=str2double(get(handles.O_C,'String'));
D_3=str2double(get(handles.O_t,'String'));
E_3=str2double(get(handles.O_R,'String'));
[P_Omega]=Omega(A_3,B_3,C_3,D_3,E_3);
setStrings(P_Omega);
case 'b4'
A_4=str2double(get(handles.A_A,'String'));
B_4=str2double(get(handles.A_t,'String'));
C_4=str2double(get(handles.A_R,'String'));
[P_Angulo]=Angulo(A_4,B_4,C_4);
setStrings(P_Angulo);
otherwise
error('Unexpected button handle')
end
function setStrings(values)
set(handle.text100,'String',values(1,1));
set(handle.text101,'String',values(1,2));
set(handle.text102,'String',values(1,3));
set(handle.text103,'String',values(1,4));
set(handle.text104,'String',values(1,5));
set(handle.text105,'String',values(1,6));
set(handle.text106,'String',values(1,7));
set(handle.text107,'String',values(1,8));
set(handle.text108,'String',values(1,9));
set(handle.text109,'String',values(1,10));
set(handle.text110,'String',values(1,11));
set(handle.text111,'String',values(1,12));
set(handle.text112,'String',values(1,13));
set(handle.text113,'String',values(1,14));
set(handle.text114,'String',values(1,15));
set(handle.text115,'String',values(1,16));
set(handle.text116,'String',values(1,17));
set(handle.text117,'String',values(1,18));
set(handle.text118,'String',values(1,19));
end
  2 Comments
Andrea Salazar
Andrea Salazar on 3 Oct 2018
I return for the old code and add that missing "S" in handles, it works!!!!!!, thank you so much for your help!, and sorry for any inconvenience
Adam Danz
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!