How to get the value from the checboxes after push a Button?

Hi guys, i have a guide, that creates a several checkboxes and a Pushbutton "enter" in the dialog box. What i want is, when i choose one of the checkboxes and then push the button "enter", the state of the checkbox, that i have chosen, should be saved in the variable "checkboxValues" and the dialog box should be closed. You can see my code, it doesn't work and showed me an error. How can i solve this problem? Thanks!
handles.figure = dialog('position',[56 224 150 155]);
handles.label={'True Positiv' 'True Negative' 'False Negative' 'Irrelevant'};
for k=1:4
h_checkbox(k) = uicontrol(handles.figure , 'Style','checkbox','String',handles.label(k), ...
'Position',[10 30*k 110 20]);
end
h.p = uicontrol(handles.figure ,'style','pushbutton','units','pixels',... 'position',[30 5 90 20],'string','OK',... 'callback',@enter_call);
function enter_call(hObject, eventdata, handles)
hCheckboxes = findobj(handles.figure, 'Style', 'checkbox');
checkboxValues = get(hCheckboxes, 'Value');
close(gcbf)

 Respuesta aceptada

Kevin Chng
Kevin Chng el 8 de Oct. de 2018
Editada: Kevin Chng el 8 de Oct. de 2018
Hi Ma Le Thai,
Hope my solution help you
clear all
close all
% Create figure & its components
figure = dialog('position',[56 224 150 155]);
handles.label={'True Positiv' 'True Negative' 'False Negative' 'Irrelevant'};
for k=1:4
handles.h_checkbox(k) = uicontrol(figure , 'Style','checkbox','String',handles.label(k), ...
'Position',[10 30*k 110 20]);
end
handles.push = uicontrol(figure ,'style','pushbutton','units','pixels',...
'position',[30 5 90 20],'string','OK');
set(handles.push,'callback',@enter_call);
guidata(figure,handles);
% Call back function
function enter_call(hObject, eventdata, handles)
handles = guidata(hObject);
checkboxValues = get(handles.h_checkbox, 'Value')
close(gcf)
end
As per my knowledge, guide does not pass handles automatically, therefore, we have to save the handle in the startup and then load the handle in the callback. I'm not sure others have better explanation for it or not.

7 comentarios

Hi Kevin, when I tried to work with your codes. it always showed me an error:
Reference to non-existent field 'h_checkbox'.
Kevin Chng
Kevin Chng el 8 de Oct. de 2018
Editada: Kevin Chng el 8 de Oct. de 2018
Did you copy my script entirely? I don't have error in running it.
May I know what version of MATLAB are you using?
Remember to clear your workspace to avoid any confusion.
I am using the R2015b. When i copy your script entirely, it shoed me another error:
Error: File: probe.m Line: 16 Column: 1
Function definitions are not permitted in this context.
Kevin Chng
Kevin Chng el 8 de Oct. de 2018
Editada: Kevin Chng el 8 de Oct. de 2018
As per I know, scripts cannot have functions in them before R2016b. There are M-script and M-function. if first line don't have the command 'function', then it is M-script.
Therefore, you copy the function and put them in new script, then save it as enter_call. Refer to attached.
Upps, thanks four your help! It's working now:)
Kevin Chng
Kevin Chng el 8 de Oct. de 2018
Editada: Kevin Chng el 9 de Oct. de 2018
Hi Mai Le, i don't have this error. I guess you want the checkboxValues return to you for everytime execute the function choosedialog().
first function :
function checkboxValues=choosedialog()
global checkboxValues
figure = dialog('position',[56 224 150 155]);
handles.label={'Irrelevant' 'False Negative' 'True Negative' 'True Positiv'};
for k=1:4
handles.h_checkbox(k) = uicontrol(figure , 'Style','checkbox','String',handles.label(k), ...
'Position',[10 30*k 110 20]);
end
handles.push = uicontrol(figure ,'style','pushbutton','units','pixels',...
'position',[30 5 90 20],'string','OK');
set(handles.push,'callback',@enter_call);
guidata(figure,handles);
waitfor(figure);
end
Second function :
function checkboxValues=enter_call(hObject, eventdata, handles)
global checkboxValues
handles = guidata(hObject);
checkboxValues = get(handles.h_checkbox, 'Value');
close(gcf)
end
Will it work for your case?
global is not highly recommend to be used. However, for your inquiries, I don't have better solution.
Do you mind accept my answer if my solution is working for you?

Iniciar sesión para comentar.

Más respuestas (1)

If you're returning handles.output, simply concatenate them all together:
handles.output = [handles.checkbox1.Value, handles.checkbox2.Value, handles.checkbox3.Value];

Categorías

Más información sobre App Building en Centro de ayuda y File Exchange.

Productos

Versión

R2015b

Etiquetas

Preguntada:

el 7 de Oct. de 2018

Comentada:

el 9 de Oct. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by