网格父级中 MATLAB 应用程序设计器中编辑字段组件的布局语法



我想在 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数组。
请参阅图形阵列。

例:

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

备注:下次发布问题时,请努力发布所有相关代码。

最新更新