9

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
Robe
  • 343
  • 5
  • 10
  • Can you not use `addpath` right before you have to load the images? Or `cd` to your app directory and use relative paths? Please see [mcve]. – sco1 Jul 05 '18 at 11:53
  • @excaza I am not able to edit the `createComponents` function, which loads the custom icons (e.g. app.Help.Icon = 'help_icon.png'). MATLAB doesn't accept icons which are not in the path: I alreadfy tried specifying the Icon File as './subfolder/help_icon.png' in the App Designer Design View. – Robe Jul 05 '18 at 13:03

1 Answers1

9

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.

sco1
  • 12,154
  • 5
  • 26
  • 48
  • Is there anything similar for when opening the **.mlapp* in the `app designer`? Running the app works great with your method, but if I want to edit it I still have to add the folders to `path` – Robe Jul 06 '18 at 12:59
  • [Add it to your path](https://www.mathworks.com/help/matlab/matlab_env/add-remove-or-reorder-folders-on-the-search-path.html) and save it. At some point you should use the search path as it's designed. – sco1 Jul 06 '18 at 15:52