如何访问另一个 .m 文件中的 MATLAB UI 函数?

问题描述 投票:0回答:1

我试图通过将一些初始化分离到单独的 .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
仍在创建,但没有按钮,并且终端给出了上面给出的错误。

matlab file user-interface matlab-figure
1个回答
0
投票

看起来你正在做两种不同事情的某种组合:

  1. 使用一个简单的函数,可以位于其自己的 .m 文件中,也可以位于主类的方法之一中:
% 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
  1. 使用类来保存对所有按钮的引用,在这种情况下,您创建的对象将是该类的属性,而不是主应用程序对象:
% 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
© www.soinside.com 2019 - 2024. All rights reserved.