如何以编程方式实现带有复选框和滚动条的动态 GUI?

How to programmatically implement a dynamic GUI with checkboxes and scrollbars?

我尝试以编程方式创建 GUI。它应显示以下内容:

右侧的复选框是静态的!!左侧复选框的数量取决于用户输入!

我尝试做的是为右侧的动态复选框创建一个面板,并为这些面板创建一个滚动条。

不幸的是,我对程序化 GUI 创建完全陌生。到目前为止,GUIDE 一切正常。

我找到了一个很好的例子

Adding scroll bar in subplots within GUI

用轴进入它,但我没有让它适应复选框:( 这是我目前的尝试。

%# create figure, panel, and slider
w = 600; h = 500;           %# width/height of figure
handles.hFig = figure('Menubar','figure', 'Resize','off', ...
    'Units','pixels', 'Position',[200 200 w h]);
handles.hPan = uipanel('Parent',handles.hFig, ...
    'Units','pixels', 'Position',[0 0 w-20 h]);
handles.hSld = uicontrol('Parent',handles.hFig, ...
    'Style','slider', 'Enable','off', ...
    'Units','pixels', 'Position',[w-20 0 20 h], ...
    'Min',0-eps, 'Max',0, 'Value',0, ...
    'Callback',{@onSlide,handles.hPan});

%# add checkbox

hcb = zeros(7,1);
clr = lines(7);

for i=1:7

    hcb(i) = addcheckbox(handles);

    pause(1)   %# slow down so that we can see the updates

end 

滑块功能不变。这是对我来说最有问题的功能:

function hcb = addcheckbox(handles)
    %# look for checkboxes
    cb = findobj(handles.hPan, 'type','checkbox');

    if isempty(cb)
        %# create first checkbox

        hcb = uicontrol(handles.hFig,'Style','checkbox',...
                'String','Display file extension',...
                'Value',1,'Position',[30 20 130 20]);

    else
        %# get height of figure
        p = get(handles.hFig, 'Position');
        h = p(4);

        %# increase panel height, and shift it to show new space
        p = get(handles.hPan, 'Position');
        set(handles.hPan, 'Position',[p(1) p(2)-h p(3) p(4)+h])

%         %# compute position of new axis: append on top (y-shifted)
%         p = get(ax, 'Position');
%         if iscell(p), p = cell2mat(p); end
%         p = [p(1,1) max(p(:,2))+h p(1,3) p(1,4)];
% 
%         %# create the new axis
%         hAx = axes('Parent',handles.hPan, ...
%             'Units','pixels', 'Position',p);
% 
%         %# adjust slider, and call its callback function
%         mx = get(handles.hSld, 'Max');
%         set(handles.hSld, 'Max',mx+h, 'Min',0, 'Enable','on')
%         %#set(handles.hSld, 'Value',mx+h)       %# scroll to new space
%         hgfeval(get(handles.hSld,'Callback'), handles.hSld, []);
    end

end

当我执行它时出现错误:

Error using findobj
Invalid handle

Error in addcheckbox (line 3)
    cb = findobj(handles.hPan, 'type','checkbox');

这个错误是有道理的,因为我没有名为复选框的句柄...在示例中 'axes' 是图中包含的预定义句柄...我如何在此上下文中集成复选框?

如上面的评论所述,uitable 对象在 GUIDE 中可用,并且已经包含复选框和滚动。虽然我绝对更喜欢编程式 GUI,但我也更喜欢不必重新创建已经运行的整个 GUI :)

这里有一个小的程序化示例(更容易 copy/paste)来说明:

dummydata = {false 'Voltage Thingey 1'; ...
             false 'Voltage Thingey 2'; ...
             false 'Voltage Thingey 3' ...
             };

mytable = uitable(...
    'Data', dummydata, ...
    'ColumnEditable', [true false], ... % Allow/disallow editing of our columns
    'ColumnWidth', {'auto', 100}, ... % Width of column, in pixels
    'ColumnName', [], ... % Get rid of column headers
    'RowName', [] ...     % Get rid of row names
    );

这给了我们: