How can I set the folder from which the MatLab App-desinger function starts as the default workspace?

I have developed a program within the Appdesinger. However, the .mlapp can only be launched and run correctly if all functions and files have been loaded into the workspace previously. When double-clicking on the program, only a part is loaded and the app does not work.
Which settings do I have to configure so that all necessary files are loaded from the local directory when the app is started directly?
can I define a startup callback function that allows this ?
Thx

 Accepted Answer

you can set paths to specific folders under properties...
properties (Access = public)
topLevelFolder = pwd;
% Get a list of all files and folders under the top level folder or user
% defined path
files = dir(topLevelFolder);
% Get a logical vector that tells which is a directory.
dirFlags = [files.isdir];
% Extract only those that are directories.
subFolders = files(dirFlags); % A structure with extra info.
% Get only the folder names into a cell array.
subFolderNames = {subFolders(3:end).name} % Start at 3 to skip . and ..
for i = 1:length(subFolderNames)
path = addpath(genpath(subFolderNames{1,i}));
end
end
You can also create startupFcn but it will run after createComponents(app).
function app = YourApp
% Create and configure components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.FigureName)
% Execute the startup function
runStartupFcn(app, @startupFcn)
if nargout == 0
clear app
end
end

5 Comments

Since all files are stored in the main directory, it was sufficient to write to the startupFcn :
app.topLevelFolder = pwd;
addpath(app.topLevelFolder)
and define topLevelFolder in the properties section. Do you think this is the proper way?
@Malte Tsz I wanted to avoid problems. This will only set the path to top level folder, it may create problems if there are subfolders.
for me it was creating problem when I was defining under startupfcn as the execution order was
1 - createComponents(app) , 2 - registerApp(app, app.FigureName) and then runStartupFcn(app, @startupFcn)...
You dont have subfolders right? Did you already tried above defining toplevelfolder in properties section and rest in startupFcn ? is it throwing any error?
Since I don't have any subfolders, and also no functions that are executed at startup, my mentioned statement has been true. Now I will test the program and see if errors occur.
Big thanks to you !
@Malte Tsz if it solves your problem, then you can accept answer! in case of error update here!
@Malte Tsz if it solves your problem, then you can accept answer! in case of error update here!

Sign in to comment.

More Answers (0)

Categories

Asked:

on 29 Aug 2022

Commented:

on 29 Aug 2022

Community Treasure Hunt

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

Start Hunting!