MATLAB App - Add path before component creation

大憨熊 提交于 2020-06-14 06:03:07

问题


I have a MATLAB App App.mlapp in a certain folder ~/myapp/. The functions it uses and some graphics used in the GUI are in ~/myapp/subfolder. In order to run App.mlapp correctly I have to each time manually add ~/myapp/subfolder to my path before launching the app.

How can I automatically add the subfolder?

I tried putting addpath(genpath(~/myapp/subfolder)); at the beginning of StartupFcn. However, as StartupFcnis called after the component creation, which already requiere some of the graphics in ~/myapp/subfolder, this approach doesn't work. The components are created using the automatically created function createComponents, which cannot be edited using the App Designer Editor.

Minimal example, as requested by excaza. To create it, open the App Designer, create a new app, add a button in Design View and specify an icon in path with Button Properties -> Text & Icon -> More Properties -> Icon File. Afterwards remove the directory of the icon from path and try running the app.

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

回答1:


While I understand MATLAB's decision to lock away a lot of the GUI's code when designing in appdesigner, I've also been fairly vocal to them about the potential significant downsides, like this one.

Soapbox aside, you can get around this by exploiting MATLAB's class property specification behavior, which initializes properties to their default properties before the rest of the class' code is executed.

In this case, we can add a dummy private variable and set it to the output of addpath:

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

Which provides the desired behavior when passed the appropriate path.



来源:https://stackoverflow.com/questions/51189516/matlab-app-add-path-before-component-creation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!