1

我想在 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

已经谢谢了!(:

4

1 回答 1

0

您需要初始化villes1gobjects.
请参阅图形数组

例子:

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

我只能猜测您使用了以下语法:上述语法创建了一个元素villes1 = zeros(1, 8);
数组。 然后创建一个“旧式”数字句柄,而不是创建一个对象。 使用数字句柄时禁止点表示法。 double
app.villes1(i) = uieditfield(...)

您需要改用以下语法: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

备注:下次发问题时,请尽量贴出所有相关代码。

于 2019-11-24T22:09:31.413 回答