필터 지우기
필터 지우기

dialog with checkboxes in gui

조회 수: 39 (최근 30일)
ram
ram 2011년 8월 9일
댓글: TEST00 2022년 4월 13일
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.

채택된 답변

Oleg Komarov
Oleg Komarov 2011년 8월 9일
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
Ryan Elson
Ryan Elson 2021년 7월 14일
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
TEST00 2022년 4월 13일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB Report Generator에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by