How to callback multiple Button Groups

I have 4 radio buttons and a static text in my Matlab GUI. I want my text window to update its string every time the radio buttons are selected. I inserted the firs two radio buttons in a button group and the other two into another button group because the total outcome possibilities should be 4.
I searched for hours and could not come up with any way to have an if-else statement which takes its values from the radio button's state (1 for selected and 0 for not selected). I tried inserting the two button groups into a new button group and write my if statements inside the new group callback but it did not work.
This is also how i coded my if-else statement:
if hObject==handles.radiobutton1 && hObject==handles.radiobutton3
set(handles.text1,'String','outcome1')
elseif hObject==handles.radiobutton1 && hObject==handles.radiobutton4
set(handles.text1,'String','outcome2')
....
....
....
New matlab user here please explain on low level :)

 Accepted Answer

Kenny - for simplicity, let's assume that you just want your static text string to be a combination of the radio button text from the selected radio button in the first button group with the selected radio button in the second button group. The first button group buttons are labelled 'first' and 'second', and the other button group buttons are labelled 'third' and 'fourth'. Depending upon which selections are made, we expect to see something like 'first~third','first~fourth', or 'second~third' or 'second~fourth' in the static text box.
Use your original method of creating two button groups only, whose names/tags are uipanell and uipanel2. Since the callback behaviour for each of these button groups is similar (update the static text field), in the SelectionChangeFcn callback for both we call a function as
updateStaticText(handles);
The above will be a function that we create which takes the handles structure as its only input. This function will then update the static text field given the user selections for the two radio buttons. The code for this function is
function updateStaticText(handles)
% get the handle to the selected radio button in uipanel1
hselectedRadioButtonGrp1 = get(handles.uipanel1,'SelectedObject');
% get the handle to the selected radio button in uipanel2
hselectedRadioButtonGrp2 = get(handles.uipanel2,'SelectedObject');
% build the string from the selected radio button text
str = [ char(get(selectedObjBGrp1,'String')) '~' char(get(selectedObjBGrp2,'String'))];
% update the static text
set(handles.text1,'String',str);
So given the handle to the selected radio button (of group 1 say), we can get its string/text using
get(selectedObjBGrp1,'String')
We do the same for the other radio button selected in the second group. The two strings are concatenated together (separated by ~), and then we update the static text box with this new string as
set(handles.text1,'String',str);
I've attached a sample GUI that does this.

More Answers (0)

Categories

Find more on Environment and Settings in Help Center and File Exchange

Asked:

on 23 Oct 2014

Commented:

on 26 Oct 2014

Community Treasure Hunt

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

Start Hunting!