我试图通过将一些初始化分离到单独的 .m 文件中来整理我的 MATLAB 应用程序代码。为此,我为每种类型的组件设置了各种文件(例如按钮、图表等的文件)。我正在尝试从按钮文件访问主初始化文件中的函数。我的代码如下,在按钮 .m 文件中如下:
classdef buttons < handle
methods
%initializes the UI
function buttonCreate(app)
%Create file load 1
app.fileload1 = uibutton(app.gridLayout, 'push');
app.fileload1.FontSize = 36;
app.fileload1.Layout.Row = [8 9];
app.fileload1.Layout.Column = 1;
app.fileload1.Text = 'Load 1';
%proceeds to create the rest of the buttons
end
end
end
现在我尝试访问主初始化文件中的
buttonCreate()
函数 initialize.m
:
classdef initialize < handle
properties
fig
gridLayout
axes
fileload1
end
methods
%initializes the UI
function init(app)
%create canvas
import buttons.*;
fig = uifigure;
fig.Position = [100 100 1920 1080];
movegui(fig,'center');
fig.Name = "Audio Editor";
%Create grid layout
gridLayout = uigridlayout(fig);
gridLayout.ColumnWidth = {'1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x'};
gridLayout.RowHeight = {'1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x', '1x'};
buttonCreate(app);
end
end
%code for calling and deleting
methods
%calls code to create canvas upon app start
function app = initialize
init(app)
end
%removes the app and deletes app.fig
function delete(app)
delete(app.fig);
end
end
end
Error in initialize/init (line 41)
buttonCreate(app);
^^^^^^^^^^^^^^^^^^
Error in initialize (line 54)
init(app)
^^^^^^^^^
这导致
UIFigure
仍在创建,但没有按钮,并且终端给出了上面给出的错误。
看起来你正在做两种不同事情的某种组合:
% in your initialise class constructor you still have
buttonCreate(app);
% In buttonCreate.m
function buttonCreate(app)
% Create file load 1
app.fileload1 = uibutton(app.gridLayout, 'push');
app.fileload1.FontSize = 36;
app.fileload1.Layout.Row = [8 9];
% ...
end
% in your initialise class constructor you create a buttons
% object and assign it to the buttons property.
% The buttons don't need to know anything about the app, just
% the target grid layout to parent the buttons
app.buttons = buttons( app.gridLayout );
% In buttons.m
classdef buttons < handle
properties
fileload1
end
methods
%initializes the UI
function obj = buttons( grid )
% Create file load 1
obj.fileload1 = uibutton(grid, 'push');
obj.fileload1.FontSize = 36;
obj.fileload1.Layout.Row = [8 9];
% ...
end
end
end