settings values of radio buttons in GUI

8 visualizaciones (últimos 30 días)
Sumera Yamin
Sumera Yamin el 23 de Ag. de 2018
Respondida: NICOLE MIN el 29 de Abr. de 2021
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.

Respuesta aceptada

Stephen23
Stephen23 el 23 de Ag. de 2018
Editada: Stephen23 el 23 de Ag. de 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 comentario
Sumera Yamin
Sumera Yamin el 24 de Ag. de 2018
Editada: Sumera Yamin el 24 de Ag. de 2018
thank you very much for your help. One more question. I understood the 1st option, run it and created the button group with options in a figure. It works well. Since i am using guide to create my GUI, how can i incorprate this in my present code. i tried to create a button group and copy your code in the function cahnging the TAG of button group, but it gives me an error, that "Undefined function or variable 'uibuttongroup2'." Alternatively, i can shift my code to the newly created figure file, but then i do not know how to add other functions (push buttons, graph) etc in this figure file since i am familiar with guide (interactive addition of buttons), i do not know how to do it pro grammatically (like you did). Please advice

Iniciar sesión para comentar.

Más respuestas (2)

Jan
Jan el 23 de Ag. de 2018
Editada: Jan el 23 de Ag. de 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 comentario
Sumera Yamin
Sumera Yamin el 23 de Ag. de 2018
Editada: Sumera Yamin el 23 de Ag. de 2018
yes i have four radio buttons. if i select 1, a part of script is to be executed, if i select other, then some other part of the script should be executed. I use else if statement to set the executable part according to value of radio button. This works fine for two options, but does not work for the third one. Unfortunately, full code is too long to paste it here. but as an example i am copying the last part of the code, in which it should plot according to button selected. Please guide, apart from value, what should be the best way to do so.
if polarity == 1
plot(x_pp, y_pp,'LineWidth',2,'marker','d')
xlabel('x (m)');
title('Plot of beam positions');
ylabel('y (m)');
elseif polarity == 0
plot(x_mp, y_mp, 'LineWidth',2,'marker','d')
xlabel('x (m)');
title('Plot of beam positions');
ylabel('y (m)');
elseif polarity == 2
plot(x_pm, y_pm, 'LineWidth',2,'marker','d')
xlabel('x (m)');
title('Plot of beam positions');
ylabel('y (m)');
elseif polarity == 3
plot(x_mm, y_mm, 'LineWidth',2,'marker','d')
xlabel('x (m)');
title('Plot of beam positions');
ylabel('y (m)');
end
here polarity i set according to value of my button group. This code does the job well for first two options but obviously not for the last two. please suggest the possible solution

Iniciar sesión para comentar.


NICOLE MIN
NICOLE MIN el 29 de Abr. de 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

Community Treasure Hunt

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

Start Hunting!

Translated by