8

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

如何自动添加子文件夹?

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

excaza 要求的最小示例。要创建它,请打开 App 设计器,创建一个新应用程序,在设计视图中添加一个按钮,并使用 Button Properties -> Text & Icon -> More Properties -> Icon File在路径中指定一个图标。然后从路径中删除图标的目录并尝试运行该应用程序。

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
4

1 回答 1

9

虽然我理解 MATLAB 决定在appdesigner.

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

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

properties (Access = private)
    oldpath = addpath('./icons')
end

当通过适当的路径时,它提供了所需的行为。

于 2018-07-05T14:07:50.517 回答