Cannot set handles in Radio Button Group
Afficher commentaires plus anciens
Hello, I am having a great deal of difficulty setting the handles for a group of radio buttons included within a panel that dictate which menu selections are available on a pop up menu. Here’s the “low-down” I am first resetting all handles to zero.
set(handles.r1_5800,'Value',0);
set(handles.r1_9800,'Value',0);
set(handles.r2_5800,'Value',0);
set(handles.r2_9800,'Value',0);
then I am calling a function which monitors the selection changes of the part panel where the radio buttons reside.
set(handles.partPanel,'SelectionChangeFcn',@partPanel_SelectionChangeFcn);
% Update handles structure
guidata(hObject, handles);
The function for the four radio buttons and how they relate to the menu is here further on in the code % --- Executes when selected object is changed in partPanel. function partPanel_SelectionChangeFcn(hObject, eventdata, handles) % hObject handle to the selected object in partPanel % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
handles = guidata(hObject);
%ctqString = [];
switch (get(eventdata.NewValue,'Tag'))
case 'r1_5800'
%
set(handles.r1_5800,'Value', 1);
ctqString = {'Noise | Offset | Gain'};
case 'r1_9800'
%
set(handles.r1_9800,'Value', 1);
ctqString = {'Noise | Offset | Gain'};
case 'r2_5800'
%
set(handles.r2_5800,'Value', 1);
ctqString = {'Noise | Offset | Gain'; 'Calibration'; 'Leakage'; 'Trim'};
case 'r2_9800'
%
set(handles.r2_9800,'Value', 1);
ctqString = {'Noise | Offset | Gain'; 'Calibration'; 'Leakage'; 'Trim'};
otherwise
% do nothing
end
set(handles.ctqMenu, 'String', ctqString);
Once this is run the handles of a single button should be set to ‘1’
Now when I hit the “Start” button and try to run the thing
if (isReadyToTest)
rev1_5800 = get(handles.r1_5800,'Value');
rev1_9800 = get(handles.r1_5800,'Value');
rev2_5800 = get(handles.r1_5800,'Value');
rev2_9800 = get(handles.r1_5800,'Value');
if ~(rev1_5800 || rev1_9800 || rev2_5800 || rev2_9800)
msgbox('Select a VyGem part in the part selection panel','Input Error', 'error');
isReadyToTest = 0;
else
isReadyToTest = 1;
end
end
all of the rev1 _5800 though rev2_9800 are still set to zero nulling out any selection I made. Any suggestions? I’ve set break points right at the if (isReadyToTest) and see that the handles weren’t set at all
Thanks Bill
1 commentaire
Fangjun Jiang
le 29 Juin 2011
Replace with
switch(get(hObject,'Tag'))
No need to set(handles.r1_5800,'Value', 1)
Réponse acceptée
Plus de réponses (1)
Fangjun Jiang
le 29 Juin 2011
0 votes
Are you constructing your GUI using GUIDE or manually writing .m code? When you put 4 radio buttons in a panel (button group), they are mutual-exclusive, so I don't think you can set all there initial value as 0. One of them has to be 1.
In the SelectionChangeFcn callback, you don't need to set the 'value' property of the radio button. The callback function of the radio button is over-written by the button group SelectionChangeFcn callback. When you select any of those four buttons, the 'value' property is automatically updated. All you need is to read the 'Tag' property of the radio button, do different things in the switch-case statement. Don't worry about the handle or value of the radio buttons.
10 commentaires
Fangjun Jiang
le 29 Juin 2011
In your current switch-case statement, you only set the value to be 1. You never reset it. Imaging you click all the radio buttons, then all of them will be 1. That is invalid. To do it correctly, every time you set one radio button to be 1, you need to re-set all the other 3.
But again, you DO NOT NEED to do this!!!
William
le 29 Juin 2011
Paulo Silva
le 29 Juin 2011
Your answer and comment makes little sense, you say in the answer 'the value property is automatically updated' and in the comment 'every time you set one radio button to be 1, you need to re-set all the other 3'.
The truth is that when you click on radio button of a group the code automatically turns the value of the other buttons to 0, the same happens in the code set(handles.radiobutton1,'Value',1) turns the other button values to 0
Fangjun Jiang
le 29 Juin 2011
I was trying to explain to Bill that he was doing it incorrectly. What if he has 10 or 100 buttons? I didn't not know if I set one to be 1, all the other will be re-set to zero. I never coded it this way. And I don't have to. See the next comment.
Fangjun Jiang
le 29 Juin 2011
The comment generated by GUIDE for your button group SelectionChangeFcn callback says:
%hObject handle to the selected object in uipanel
That means get(hObject,'Tag') is the 'Tag' name of the radio button. So use switch-case statement, you easily knows which radio button is selected.
switch get(hObject,'Tag')
case 'radiobutton1'
case 'radiobutton2'
end
Paulo Silva
le 29 Juin 2011
No matter how many buttons with a little knowledge you can set the values for them all inside just a simple line of code.
Fangjun Jiang
le 29 Juin 2011
But what's the point to set that value if its automatically done already. Don't you agree that using switch get(hObject,'Tag') is sufficient and a better approach?
Paulo Silva
le 29 Juin 2011
Fangjun Jiang my comment was just for your 'What if he has 10 or 100 buttons?' question.
Yes get(hObject,'Tag') seems to do the same which seems strange because hObject should be the handle for the Radio Button Group, I don't call it a better approach but it does work.
Fangjun Jiang
le 29 Juin 2011
It was mentioned in the comment generated by GUIDE for the SelectionChangeFcn for the button group.
%hObject handle to the selected object in uipanel
I use GUIDE to develop my GUI. So in my SelectionChageFcn, disp(get(hObject,'Tag')) is enough to show how button group works. What is your usual way to do things differently based on button group selection?
Paulo Silva
le 29 Juin 2011
You are correct and I didn't noticed that before, never used the SelectionChageFcn until now, my only use of the button group is to have only one radio button selected, usually use the value of radiobuttons inside if conditions just before executing important code.
Catégories
En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!