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

时间:2015-09-24 10:21:59

标签: matlab user-interface checkbox scrollbar figure

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

enter image description here

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

我尝试做的是创建右侧动态复选框的面板和这些面板的滚动条。

不幸的是,我对程序化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');

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

1 个答案:

答案 0 :(得分:0)

如上面的评论所述,GUIDE中提供了uitable个对象,并且已经包含了复选框和滚动条。虽然我绝对更喜欢程序化GUI,但我也不想重新创建已经运行的整个GUI:)

这是一个小型的程序化示例(更容易复制/粘贴)来说明:

let f (x:int) (y:int) : int = x + y
let g (x:bool) (y:bool) (z:bool) : bool = x & y & z

type T = T with
    static member ($) (T, x) = fun y   -> f x y
    static member ($) (T, x) = fun y z -> g x y z

let inline myFunc x y = (T $ x) y

let result1 = myFunc 3 5
let result2 = myFunc true false true

这给了我们:

yay!