Button Group / make radiobutton exclusive

4 ビュー (過去 30 日間)
Max Müller
Max Müller 2014 年 9 月 24 日
編集済み: Adam 2014 年 9 月 25 日
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 件のコメント
Max Müller
Max Müller 2014 年 9 月 24 日
yes i did,but my radiobutton had already Callbacks. Is this the reason it dont works ?
Adam
Adam 2014 年 9 月 24 日
編集済み: Adam 2014 年 9 月 24 日
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

サインインしてコメントする。

採用された回答

Adam
Adam 2014 年 9 月 24 日
編集済み: Adam 2014 年 9 月 24 日
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 件のコメント
Adam
Adam 2014 年 9 月 25 日
編集済み: Adam 2014 年 9 月 25 日
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!!
Max Müller
Max Müller 2014 年 9 月 25 日
ok. u are right

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by