MATLAB应用程序 - 在组件创建之前添加路径

Rob*_*obe 5 matlab matlab-app-designer

我在某个文件夹中有一个MATLAB App App.mlapp~/myapp/.它使用的功能和GUI中使用的一些图形都在~/myapp/subfolder.为了正确运行App.mlapp,我必须~/myapp/subfolder在启动应用程序之前每次手动添加到我的路径.

如何自动添加子文件夹?

我试着把它放在addpath(genpath(~/myapp/subfolder));开头StartupFcn.但是,正如StartupFcn在组件创建之后调用的那样,已经需要一些图形~/myapp/subfolder,这种方法不起作用.使用自动创建的功能创建组件createComponents,无法使用App Designer编辑器对其进行编辑.

最小的例子,根据excaza的要求.要创建它,请打开App Designer,创建一个新应用程序,在设计视图中添加一个按钮,并在路径中使用按钮属性 - >文本和图标 - >更多属性 - >图标文件指定一个图标 .然后从路径中删除该图标的目录,并尝试运行该应用程序.

classdef app1 < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure  matlab.ui.Figure
        Button    matlab.ui.control.Button
    end

    % App initialization and construction
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create UIFigure
            app.UIFigure = uifigure;
            app.UIFigure.Position = [100 100 640 480];
            app.UIFigure.Name = 'UI Figure';

            % Create Button
            app.Button = uibutton(app.UIFigure, 'push');
            app.Button.Icon = 'help_icon.png';
            app.Button.Position = [230 321 100 22];
        end
    end

    methods (Access = public)

        % Construct app
        function app = app1

            % Create and configure components
            createComponents(app)

            % Register the app with App Designer
            registerApp(app, app.UIFigure)

            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
Run Code Online (Sandbox Code Playgroud)

exc*_*aza 5

虽然我理解MATLAB在设计时锁定了很多GUI代码的决定appdesigner,但我也对他们说明潜在的重大缺点,比如这个.

除了Soapbox之外,您可以通过利用MATLAB的类属性规范行为来解决这个问题,该行为在执行其余类代码之前将属性初始化为其默认属性.

在这种情况下,我们可以添加一个虚拟私有变量并将其设置为以下输出addpath:

properties (Access = private)
    oldpath = addpath('./icons')
end
Run Code Online (Sandbox Code Playgroud)

在传递适当的路径时,它提供了所需的行为.