settings values of radio buttons in GUI
10 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
hello MATLAB community, is it possible to set values of radio buttons in GUI other than 0 or 1. I have a panel with four options (lets say A, B, C, D). According to selected option, the relevant part of my script should execute. i set values of radio buttons to 0, 1, 2, 3 respectively and used elseif statement to execute the relevant part of the script. it work fine with value 0 and 1 , but gives me an error saying "Warning: radiobutton control requires that Value be equal to either Min or Max Control will not be rendered until all of its parameter values are valid". what would be the best way to deal with such problem. Thank you very much for the help.
0 Commenti
Risposta accettata
Stephen23
il 23 Ago 2018
Modificato: Stephen23
il 23 Ago 2018
Depending on how your code works, you could use one of these two methods. Both of them let you set an arbitrary variable in each button and return the one from the select button.
Method ONE: Create a uibuttongroup, set each button's UserData to any variable that you want, then in your code simply use the button group's SelectedObject property to return the selected button object, from which you can trivially get the UserData. Here is a working example:
N = 4;
bg = uibuttongroup('Position',[0,0,0.2,1]);
for k = 1:N
uicontrol(bg,'Style','radiobutton',...
'String',sprintf('Option %d',k-1),'UserData',k-1,...
'units','normalized', 'Position',[0,(k-1)/N,1,1/N]);
end
Run that code to create some buttons. Click whichever button you want. Now simply poll the selected button to get the UserData that you specified in the selected button:
>> get(get(bg,'SelectedObject'),'UserData')
ans =
2
Method TWO: Create a uibuttongroup, set each button's UserData to any variable that you want, and define the buttongroup's SelectionChangeFcn callback to get that variable. Here is a working example:
function temp0()
N = 4;
bg = uibuttongroup('Position',[0,0,0.2,1]);
for k = 1:N
uicontrol(bg,'Style','radiobutton',...
'String',sprintf('Option %d',k-1),'UserData',k-1,...
'units','normalized', 'Position',[0,(k-1)/N,1,1/N]);
end
set(bg,'SelectionChangeFcn',@bselection)
end
function bselection(~,e)
val = get(e.NewValue,'UserData')
end
This would be useful with nested functions, where the user would change the value of a variable in the parent workspace by triggering that callback.
1 Commento
Più risposte (2)
Jan
il 23 Ago 2018
Modificato: Jan
il 23 Ago 2018
No, radio buttons have only 2 states by definition: They are either checked or unchecked. It is not clear, what you want to achieve by setting 4 different states, because you can see it directly in the optical representation of a radio button, that there are 2 states only.
Do you mean, that you have 4 radio buttons? Then the state is represented in the enabled button, not in the property 'Value'.
1 Commento
NICOLE MIN
il 29 Apr 2021
% --- Executes during object creation, after setting all properties.
function Pregnancies_CreateFcn(hObject, eventdata, handles)
% hObject handle to Calculate (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
high_glucose =350;
low_glucose=0;
mean_Glucosefeat=121;
input_fuzzy_glucose=175;
high_BMI=60;
low_BMI=0;
mean_BMI=39;
input_fuzzy_BMI=30;
max_rule1=175;
max_use1=350;
max_rule2=30;
max_use2=60;
before_meal=95;
number1=str2double(get(handles.Glucose,'string'));
number2=str2double(get(handles.BMI,'string'));
if(number1>before_meal && number1>mean_Glucosefeat && number2>=max_rule2)
disp('high risk of diabetic')
elseif (number1<before_meal && number1<=mean_Glucosefeat && number2<max_rule2)
disp('normal');
elseif(number1<before_meal && number1<=mean_Glucosefeat && number2<max_rule2)
disp('danger');
else
[];
end
set(handles.Diagnostic,'string',disp())
function Diagnosis_Callback(hObject, eventdata, handles)
% hObject handle to Diagnosis (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of Diagnosis as text
% str2double(get(hObject,'String')) returns contents of Diagnosis as a double
set(handles.Diagnostic,'string',disp())
may i know where the code went wrong. i wanted to produce the diagnostic result in diagnostic text field box hence i have crated a callback function. under the radiobutton(Pregnancies) i have set the rule. by the disply of result is not working in the textfield
0 Commenti
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!