How to access external helper functions in apps created using App Designer?

10 views (last 30 days)
I created a simple app using App Designer called 'Appexample'  in a folder called  'ExampleFolder' with the following file structure : 
ExampleFolder
|
|
------ Appexample.mlapp
------ folder1
|
|
------ helperscript1.m
ExampleFolder2
|
|
---------helperscript2.m
Another folder called 'ExampleFolder2' (external to 'ExampleFolder1')  contains a helper function called 'helperscript2.m' 
My question is how can I access helper functions, 'helperscript1' and 'helperscript2.m' from the app? 
 

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 13 Jun 2023
For calling  'helperscript1.m' and 'helperscript2.m' functions from the app, the functions have to be on the MATLAB path as they are not in the same folder as the MLAPP file. MATLAB first searches the current directory before looking at the MATLAB path. Please refer to the following documentation page for more details on how MATLAB decides where to look for functions:
 
You can add the path of the helper function by calling the 'addpath' function from within the 'startupFcn' of the app. Caution has to be taken when calling 'addpath' from the app as 'addpath' expects all input paths to be either a full path or relative to MATLAB's current folder.
You can use the 'pwd' command to get the current MATLAB directory path. The following lines of code can be used in the 'startupFcn' function to add 'folder1' to MATLAB path when you open the app. That way the 'folder1', and thus its contents, will be accessible by the app.
helperpath = fullfile(pwd, 'folder1');
addpath(helperpath);
Similarly you can add 'ExampleFolder2' in the path, by substituting the 'helperpath' variable above, with the a string of the full path to the folder.

More Answers (0)

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!