'Is there an easier way to disable all the buttons in matlab app designer?

I wanted to know if there is an easier way to disable or enable all the buttons without calling each one separately. I was hoping to be able to press a start button which would then enable all the buttons in the app.

        app.Button_1.Enable = "off";
        app.Button_2.Enable = "off";
        app.Button_3.Enable = "off";
        app.Button_4.Enable = "off";
        app.Button_5.Enable = "off";
        app.Button_6.Enable = "off";
        app.Button_7.Enable = "off";
        app.Button_8.Enable = "off";
        app.Button_9.Enable = "off";


Solution 1:[1]

You need to create an array of handles to the components you want to affect, then loop through that array. The example below works assuming that all of your buttons are placed directly onto the UIFigure and not in containers (no panels, grid layouts, tab groups). If you do have any panels/etc. then you need to get the handles for their children also, and add them to your array of handles.

(The example below also skips disabling anything other than buttons, to show how you would do that. If you want to disable all components (including edit fields, etc.), then just remove that if statement.

Edit: I should also point out that App Designer now has functions like uiconfirm() that are modal, and will therefore disable all other components until the user interacts with them.

    % Code that executes after component creation
    function startupFcn(app)
        allComponents = app.UIFigure.Children; % get a list of components that are direct children of the UIFigure
        
        for ii = 1:numel(allComponents) % loop through all the components
            thisComponent = allComponents(ii);
            if strcmpi(thisComponent.Type, 'uibutton') % disable buttons only
                thisComponent.Enable = 'off';
            end
        end
    end

    % Button pushed function: Button
    function ButtonPushed(app, event)
        allComponents = app.UIFigure.Children; % get a list of components that are direct children of the UIFigure
        
        for ii = 1:numel(allComponents) % loop through all the components
            thisComponent = allComponents(ii);
            if strcmpi(thisComponent.Type, 'uibutton') % enable buttons only
                thisComponent.Enable = 'on';
            end
        end
    end

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sentiment