Button Group / make radiobutton exclusive

4 views (last 30 days)
Max Müller
Max Müller on 24 Sep 2014
Edited: Adam on 25 Sep 2014
Hey guys, i have create a bunch of radiobuttons in one GUI. Now I want to make each radiobutton exclusive.... only one button can be active... if the use activates a new radiobutton the old one`s value is 0 (inactive). i have tried to use the SelectionChangeFcn but somehow it doesnt work. Can u guys give me an example with 2 radiobuttons in one panel ? i am going on to search for a solution.
Oh and pls use GUIDE in ur explaination.
  3 Comments
Max Müller
Max Müller on 24 Sep 2014
yes i did,but my radiobutton had already Callbacks. Is this the reason it dont works ?
Adam
Adam on 24 Sep 2014
Edited: Adam on 24 Sep 2014
Actually I just took a look at creating a new radio button group. It looks like it has changed in recent versions of Matlab so you don't have to do so much manually.
Now when you drop radio buttons into a group your callback will be set to @manageButtons as shown below. You should be able to leave that as is and just add a SelectionChangeFcn. In previous versions I seem to remember having to get rid of callbacks manually when I added a SelectionChangeFcn

Sign in to comment.

Accepted Answer

Adam
Adam on 24 Sep 2014
Edited: Adam on 24 Sep 2014
This is a simple example from some code I just put together. A mutually exclusive radio box with two options and the SelectionChangeFcn that just prints to command window which is selected. Obviously you can do more complex stuff and store things on handles as usual within the callback.
% --- Executes when selected object is changed in uipanel1.
function uipanel1_SelectionChangeFcn(hObject, eventdata, handles)
str = get( hObject, 'String' );
if strcmp( str, 'Button 1' )
disp( 'Button 1' )
else
disp( 'Button 2' )
end
If it is useful here are a couple of images of the same in guide. There are other ways of programming the SelectionChangeFcn. Sometimes I use the radiobutton tags in my switch, but the above works ok. In general I think I would favour using tag though than strcmp against the string. The example works the same, just get 'Tag' instead of 'String'. It is less prone to typos than comparing a string though also a little less friendly to read.
%
  5 Comments
Adam
Adam on 25 Sep 2014
Edited: Adam on 25 Sep 2014
The 'Value' field for each individual radiobutton controls its state so you can set all radiobuttons to a Value of 0 to achieve this. It appears nothing stops you setting all of them to 1 either though I thought I remembered the mutual exclusivity kicking in here when you set one to automatically unset the other.
Personally I would not recommend setting all to 0 as an initial state (and obviously never set all or more than one to a non-zero value).
This represents what would normally be considered an invalid state for radio buttons and one which can never be returned to after the user makes a selection.
If you really want this I would probably recommend you add a checkbox which disables the radiobuttons in the case where you want none of them to be active. This then maintains the internal consistency of the radiobuttons in all situations rather than having an odd state that can only occur at initialisation.
I have never tried it because it is a very bad idea, but I imagine you may be able to over-ride the mutual exclusivity control on radio buttons by setting their individual 'Value' fields programatically at a later stage too, but don't go down that route!!

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks 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!