MATLAB: How to get the value from the checboxes after push a Button

MATLABmatlab gui

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)

Best Answer

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.
Related Question