Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB App - Add path before component creation

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
like image 243
Robe Avatar asked Jul 05 '18 10:07

Robe


People also ask

How do I add a path to MATLAB app?

addpath (MATLAB Functions) As an alternative to the addpath function, use the Set Path dialog box. To open it, select Set Path from the File menu in the MATLAB desktop. addpath(' directory') prepends the specified directory to the current MATLAB search path, that is, it adds them to the top of the path.

How temporarily add path in MATLAB?

You can use path(path_to_add,path) to add a path to the current path variable.

What does it mean to add to path in MATLAB?

Once the list of directories and subdirectories is created, it is passed to addpath(), which adds the list (by default) to the beginning of the list of directories that is the MATLAB "path". The MATLAB "path" is the list of directories that is to be searched to find .

What is Mlapp?

MLApp is a Python library for building scalable data science solutions that meet modern software engineering standards.


1 Answers

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.

like image 183
sco1 Avatar answered Oct 27 '22 03:10

sco1