How to pass function to radio button in a button group created using guide in MATLAB?

vijisai picture vijisai · Apr 12, 2010 · Viewed 17.1k times · Source

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.

Answer

Azim J picture Azim J · Apr 12, 2010

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.