views:

346

answers:

2

I have created a button group with four radio buttons and a push button using guide.

There are four functions, one for each radio button written separately.

  1. How do you to call these functions from respective radio buttons.
  2. When a push button is pressed, the function associated with the active radio button should execute.
+1  A: 

Crash course on GUI's starts... now:

If you're using guide, then when you save your figure mygui.fig, the M-file should be automatically generated as mygui.m. Open up mygui.m and enter your code under the radio button callback function. Any variables that you want initialized when you start the program should be defined under the opening function. Make sure you update the handles structure at the end of each callback, with the command guidata(hObject,handles).

For example, if you wanted two mutually exclusive radio buttons (when you select one, the other de-selects, or when you de-select one, the other is selected), enter the following code under their callbacks:

function radiobutton1_Callback(hObject, eventdata, handles)
if get(handles.hObject,'Value')
    set(handles.radiobutton2,'Value',0)
else
    set(handles.radiobutton2,'Value',1)
end
guidata(hObject,handles);

and

function radiobutton2_Callback(hObject, eventdata, handles)
if get(hObject,'Value')
    set(handles.radiobutton1,'Value',0)
else
    set(handles.radiobutton1,'Value',1)
end
guidata(hObject,handles);

And initialize radio button one to be selected under the opening function:

set(handles.radiobutton1,'Value',1)
set(handles.radiobutton2,'Value',0)
Doresoom
hmmm, just noticed first line of both your functions "handles.b2 = ..." . The _handles_ structure contains the handles for each gui object. So why are you resetting the handle to the object tagged b2? Perhaps you should just have "b2_value = get(...)" instead?
Azim
You're right, I was getting sloppy with my code. Edited to reflect your suggestions.
Doresoom
+2  A: 

A solution for the Button Group Callback: SelectionChangeFCN

Use the Selection Change callback property (right click on the Button Group and select View Callbacks->SelectionChangeFcn) of the uipanel. The eventdata argument contains the handles to the current and previously selected radiobutton. The eventdata argument is a structure with the following fields:

  • EventName
  • OldValue
  • NewValue

So, depending on the value of eventdata.NewValue; for example

function uipanel1_SelectionChangeFcn(hObject,eventdata,handles)
...
newButton=get(eventdata.NewValue,'tag');
switch newButton
     case 'radiobutton1'
         % code for radiobutton 1 here
     case 'radiobutton2'
         % code for radiobutton 2 here
     ...
end
...

A solution for the push button callback

The callback for your push button could have something along the lines of

function button1_Callback(hObject,eventdata,handles)
h_selectedRadioButton = get(handles.uipanel1,'SelectedObject');
selectedRadioTag = get(h_selectedRadioButton,'tag')
switch selectedRadioTag
   case 'radiobutton1'

   case 'radiobutton2'
   ...
end

I also refer you to the MATLAB documentation for more information on Handle Graphics and building graphical user interfaces.

Azim
How do you guys always beat me to these??? I was just typing up a solution like this one... :)
MatlabDoug
@Azim Hmm... I've never used your approach before, but I have the feeling it's technically the right way to do it. +1 for showing me something new.
Doresoom
@MatlabDoug Just lucky I noticed the new question in my RSS-reader. It's definitely getting harder to catch unanswered questions.
Azim
@Doresoom thanks. Both approaches should work. Assigning a callback to each radio button might have advantages as well.
Azim