-1

I try to create a GUI programmatically. It shall show the following things:

enter image description here

where the checkboxes at the right side are static!! the number of checkboxes on the left depends on the userinput!

What i try to do is to create is a panel for the dynamic checkboxes on the right hand side and a scrollbar for those panels.

Unfortunately I am completely new to programmatic GUI creation. So far everything worked fine with GUIDE.

I found a good example

Adding scroll bar in subplots within GUI

for getting into it with axes but i don't get it adapted to checkboxes:( This is my Try so far.

%# 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 

The slider function is not changed. And here is the most problematic function for me:

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

When i execute it i get the error:

Error using findobj
Invalid handle

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

This error makes sense becaue i dont have a handle named checkboxes...in the example 'axes' is a predefined handle contained in the figure...how can i integrate checkboxes in this context?

Cœur
  • 37,241
  • 25
  • 195
  • 267
john
  • 131
  • 2
  • 9
  • The provided code does not replicate the error described. If your GUIDE GUI functions as desired, why are you making a programmatic GUI? – sco1 Sep 24 '15 at 11:15
  • Oha, you are right! is there a possibility of editing this question? i missed considering things...How can GUIDE handle a dynamic creation of checkboxes? The corresponding figure has to be predefined or am i wrong? – john Sep 24 '15 at 11:49
  • Yes, that's correct. However, one alternative I would suggest is using [`uitable`](http://www.mathworks.com/help/matlab/ref/uitable.html), which you can implement in GUIDE and can contain checkboxes. It also handles the scrolling for you. I much prefer programmatic GUIs personally but there's no need to redo the entire GUI here. – sco1 Sep 24 '15 at 12:02
  • Thank you very much excaza!! uitable seems to provide everything i need! – john Sep 24 '15 at 12:08

1 Answers1

0

As mentioned in the comments above, uitable objects are available in GUIDE and already incorporate checkboxes and scrolling. While I definitely prefer programmatic GUIs, I also prefer not having to recreate an entire GUI that already functions :)

Here's a small, programmatic example (easier to copy/paste) to illustrate:

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
    );

Which gives us:

yay!

sco1
  • 12,154
  • 5
  • 26
  • 48