dialog with checkboxes in gui
25 views (last 30 days)
Show older comments
Hi! I want to make a dialog box with a variable number of checkboxes. From my main GUI, I can create a figure, then I create checkboxes in that figure with "uicontrol...", and one button for "OK". But, how do I associate a callback with that button? and how do I read the status of those checkboxes?
I have also tried to make a new window with GUIDE, but where in the code should I create the checkboxes? and how do I give to the called gui the number of checkboxes?
Or someone please tell me where in the documentation is this covered. Thanks a lot.
0 Comments
Accepted Answer
Oleg Komarov
on 9 Aug 2011
In the following example I build a basic gui which has two checkboxes and a pushbutton.
The only action assigned to the push button is to check which checkboxes are selected (save as myGUI and execute myGUI in the command window):
function myGUI
% Create figure
h.f = figure('units','pixels','position',[200,200,150,50],...
'toolbar','none','menu','none');
% Create yes/no checkboxes
h.c(1) = uicontrol('style','checkbox','units','pixels',...
'position',[10,30,50,15],'string','yes');
h.c(2) = uicontrol('style','checkbox','units','pixels',...
'position',[90,30,50,15],'string','no');
% Create OK pushbutton
h.p = uicontrol('style','pushbutton','units','pixels',...
'position',[40,5,70,20],'string','OK',...
'callback',@p_call);
% Pushbutton callback
function p_call(varargin)
vals = get(h.c,'Value');
checked = find([vals{:}]);
if isempty(checked)
checked = 'none';
end
disp(checked)
end
end
3 Comments
Ryan Elson
on 14 Jul 2021
Hi @Oleg Komarov, thanks for the code, really useful, I am trying to make a version which outputs the vector of checked buttons and have tried multiple things but have not managed to get it to work and was wondering if you might be able to help (although I appreciate that your original post was 10 years ago!)
My aim is to call the function using a command like the following:
checked = checkBoxGui({'option 1', 'option 2', 'option 3'})
function checked = checkBoxGui(options)
% determine the number of handles
nOptions = numel(options);
boxHeight = nOptions * 30;
boxWidth = 200;
checkBoxHeights = linspace(30, boxHeight-30, nOptions);
checkBoxHeights = flip(checkBoxHeights);
disp('Check box heights')
disp(checkBoxHeights)
% Create figure
h.f = figure('units','pixels','position',[400,400,boxWidth,boxHeight],...
'toolbar','none','menu','none');
% Create checkboxes
for op = 1:nOptions
h.c(op) = uicontrol('style','checkbox','units','pixels',...
'position',[10,checkBoxHeights(op),200,15],'string',options{op});
end
% Create OK pushbutton
h.p = uicontrol('style','pushbutton','units','pixels',...
'position',[40,5,70,20],'string','OK',...
'callback',@p_call);
% Pushbutton callback
function checked = p_call(varargin)
vals = get(h.c,'Value');
checked = find([vals{:}]);
close(h.f)
if isempty(checked)
checked = 'none';
end
disp(checked)
end
end
TEST00
on 13 Apr 2022
use 'uiwait' with delete
add 1 line inside function 'p_call' like:
h.checked = checked;
and add 3 line last section of function 'checkBoxGui' like:
uiwait(h.f);
delete(h.f);
check = h.checked;
then, you can get output value of what you checked.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!