How to assign a value using a RADIO Button in the GUI

I want to use 2 radio buttons in a GUI in this way:
radiobutton1 -> acceleration = 1; radiobutton2 -> acceleration = -1;
Then I want to use this "acceleration" within other functions that are used by the GUI.
I've no problems with "Edit text", "Push buttons" and others, but I'm struggling with the Radio Button. For example I can use a value with the "Edit text" simply buy writing
value = handles.EDIT_TEXT;
But I'm not able to do the same with a radio button.
Thanks in advance!

 Akzeptierte Antwort

debasish
debasish am 20 Jun. 2012
1.Create the two radio buttons in a button panel
2.Set the properties of each radio button.(tag,font,string,etc).lets assume tags be 'accel1_radiobutton' and 'accel2_radiobutton'.
Set the tag of button panel to 'acceleration_buttongroup'.
3.Add this line of code to the opening function
set(handles.acceleration_buttongroup, 'SelectionChangeFcn', @acceleration_buttongroup_SelectionChangeFcn);
4.Next,add the following function at the very end of .m file
function acceleration_buttongroup_SelectionChangeFcn(hObject, eventdata)
handles=guidata(hObject);
switch get(eventdata.Newvalue, 'Tag' )
case 'accel1_radiobutton'
handles.value= 1; %%any handle where value is to be stored
case accel2_radiobutton
%%similarly set the value '-1'

5 Kommentare

I get this error, if I set handles.value = 1 (or handles.ACCELERATION = 1)
Reference to non-existent field 'value'. (or 'ACCELERATION')
Error in my_gui_function>Start_Callback (line 865)
acceleration = handles.value;
At the end of the code that debasish shows, add
guidata(hObject, handles);
It works!
My fault for writing h0bject instead of hObject!
Thanks to the both of you!
PS for anybodyelse who's reading this:
it is:
switch get(eventdata.NewValue, 'Tag' ) instead of .Newvalue
and then of course just write "end" after your last "case".
Hi guys. So far so good. But i have a problem thow. The handles structure isnt gettin updateed with the new variable after exiting the buttongroup function. ANy recomendations?
thanks @debasish!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Future Science
Future Science am 20 Jun. 2012

0 Stimmen

Another question: even tough the radio button appears to be selected by default, if I don't manually select "acceleration2" and then "acceleration1" it won't load a value.
Is there a workaround for this?

3 Kommentare

After you have done the set() that debasish mentioned in step 3, then invoke
acceleration_buttongroup_SelectionChangeFcn( handles.acceleration_buttongroup, struct( 'NewValue', handles.accel1_radiobutton ) )
That is, you will be calling the callback routine with artificial values.
Thanks, very helpful as usual!
thank you @walter roberson! really helped! was stuck with the same problem.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu MATLAB finden Sie in Hilfe-Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by