网格父级中 MATLAB App 设计工具中编辑字段组件的布局语法

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

我想在 MATLAB App Designer 中创建编辑字段的网格后立即指定布局。

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

我试过简单地使用 [r,c] 但这种语法似乎不正确。我用谷歌搜索了我的问题,这是通过创建编辑字段并在之后通过点索引指定行和列来指定行和列的唯一方法:

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

但是,我不能使用它,因为我实际上是在 for 循环中生成编辑字段,并且禁止以这种方式进行点索引:

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

已经谢谢了! (:

您需要将 villes1 初始化为 gobjects 的数组。
参见 Graphics Arrays

示例:

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

我只能猜测您使用了以下语法:villes1 = zeros(1, 8);
上面的语法创建了一个包含 double 个元素的数组。
然后 app.villes1(i) = uieditfield(...) 创建一个 "old style" 数字句柄,而不是创建一个对象。
使用数字句柄时禁止点符号。

您需要改用以下语法:villes1 = gobjects(1, 8);.
现在点索引应该可以工作了。


这是完整的代码(包括生成的代码):

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

备注:下次您post提问时,请尽量post所有相关代码。