Search code examples
matlabmatlab-app-designer

Layout syntax of an Edit Field component in MATLAB App Designer in a grid parent


I would like to specify the layout in a grid of an edit field as soon as I create it in MATLAB App Designer.

app.villes1 = uieditfield(app.GHIetPOA_grid, 'text', 'HorizontalAlignment', 'center', ...
              'Editable', 'on', 'Layout', **???**);

I tried using simply [r,c] but this syntax does not seems right. I googled my problem an only founded ways to specify row and column by creating the edit field and specifying the row and column after by dot indexing :

app.villes1.Layout.Row = 2; 
app.villes1.Layout.Column = i+2; 

However, I can't use this because I am actually generating edit field in a for loop and dot indexing is forbidden this way :

function initVilles1(app)
    for i=1:8
        app.villes1(i) = uieditfield(app.GHIetPOA_grid, 'text', 'HorizontalAlignment', ...
        'center', 'Editable', 'on');
        app.villes1(i).Layout.Row = 2; 
        app.villes1(i).Layout.Column = i+2; 
     end
end

Thanks already! (:


Solution

  • You need to initialize villes1 as array of gobjects.
    See Graphics Arrays.

    Example:

    properties (Access = private)
        GHIetPOA_grid  matlab.ui.container.GridLayout
        villes1 = gobjects(1, 8);
    end
    

    I can only guess that you used the following syntax: villes1 = zeros(1, 8);
    Above syntax creates an array of double elements.
    Then app.villes1(i) = uieditfield(...) creates an "old style" numeric handle, instead of creating an object.
    Dot notation is forbidden when using numeric handles.

    You need to use the following syntax instead: villes1 = gobjects(1, 8);.
    Now the dot indexing should work.


    Here is the complete code (include generated code):

    classdef app1 < matlab.apps.AppBase
    
        % Properties that correspond to app components
        properties (Access = public)
            UIFigure  matlab.ui.Figure
        end
    
    
        properties (Access = private)
            Panel          matlab.ui.container.Panel
            GHIetPOA_grid  matlab.ui.container.GridLayout
            villes1 = gobjects(1, 8);
        end
    
        methods (Access = private)
    
            function initVilles1(app)
                for i=1:8
                    app.villes1(i) = uieditfield(app.GHIetPOA_grid, 'text', 'HorizontalAlignment', ...
                    'center', 'Editable', 'on');
                    app.villes1(i).Layout.Row = 2; 
                    app.villes1(i).Layout.Column = i+2; 
                 end            
            end
        end
    
    
        % Callbacks that handle component events
        methods (Access = private)
    
            % Code that executes after component creation
            function startupFcn(app)
                % Create Panel
                app.Panel = uipanel(app.UIFigure);
                app.Panel.Title = 'Panel';
                app.Panel.Position = [42 198 1026 464];
    
                % Create GridLayout
                app.GHIetPOA_grid = uigridlayout(app.Panel);
                app.GHIetPOA_grid.RowHeight = {'1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x'};
    
                %app.villes1 = uieditfield(app.GHIetPOA_grid, 'text', 'HorizontalAlignment', 'center', 'Editable', 'on');
                initVilles1(app);
            end
        end
    
        % Component initialization
        methods (Access = private)
    
            % Create UIFigure and components
            function createComponents(app)
    
                % Create UIFigure and hide until all components are created
                app.UIFigure = uifigure('Visible', 'off');
                app.UIFigure.Position = [100 100 1118 692];
                app.UIFigure.Name = 'UI Figure';
    
                % Show the figure after all components are created
                app.UIFigure.Visible = 'on';
            end
        end
    
        % App creation and deletion
        methods (Access = public)
    
            % Construct app
            function app = app1
    
                % Create UIFigure and components
                createComponents(app)
    
                % Register the app with App Designer
                registerApp(app, app.UIFigure)
    
                % Execute the startup function
                runStartupFcn(app, @startupFcn)
    
                if nargout == 0
                    clear app
                end
            end
    
            % Code that executes before app deletion
            function delete(app)
    
                % Delete UIFigure when app is deleted
                delete(app.UIFigure)
            end
        end
    end
    

    Remark: Next time you post a question, please make an effort to post all the relevant code.