kingnull
kingnull

Reputation: 79

can I iterate through uicontrols in a figure?

I want to iterate through uicontrol toggle buttons in a figure so a recursive function can check adjacent uicontrol toggle buttons and modify them accordingly.

I've created a uicontrol togglebutton grid within a figure like so: function create_field(hparent, numX, numY, width, padding)

       set(hparent, 'Units', 'pixels');
       ppos = get(hparent, 'Position');
       ppos(3) = numX*width + (numX-1)*padding;
       ppos(4) = numY*width + (numY-1)*padding;
       set(hparent, 'Position', ppos);

       for i = 1:numX
            for j = 1:numY
                    bPos = [                  % Button spec:
                       (i-1)*(width+padding)  %  - X
                       (j-1)*(width+padding)  %  - Y
                       width                  %  - W
                       width                  %  - H
                    ];
                    uicontrol(                              ...
                       'Units',     'pixels',               ...
                       'Tag',       sprintf('X%dY%dS%d',i,j,state),  ...
                       'Style',     'togglebutton',         ...
                       'Parent',    hparent,                ...
                       'Position',  bPos,                   ...
                       'Callback',  @reveal                 ...
                    );
            end;
    end;
end

is there anyway to iterate through the uicontrols after they've been created ? alternatively if that is not possible how would I add the uicontrolls from a cell array into a figure?

Upvotes: 1

Views: 65

Answers (1)

CitizenInsane
CitizenInsane

Reputation: 4855

You might use findobj for that ...

For instance here is how to find all toggle buttons only in a figure:

toggleBtns = findobj(myFig, 'Style', 'togglebutton');

Edit

Alternatively, you can save all toggle buttons handles in a cell array as suggested and then find them back later with guidata:

% Create buttons
cellToggleBtns{end+1} = uicontrols(...);

% Save their handles within figure data
data = guidata(fig);
data.AllToggleBtns = cellToggleBtns;
guidata(fig, data);

Later in code or callback:

% Retreive toggle buttons handles
data = guidata(fig);
toggleBtns = data.AllToggleBtns; 

Upvotes: 1

Related Questions