Directorry for Saving Settings from App Designer
29 views (last 30 days)
Show older comments
I have an application written for Appdesigner. I want to be able to save user settings when they exit, and reload the next time they restart. I would like the saved data to be stored in the same directory as the app. Since the app directory is in the search path, the user can be starting the app from anywhere. How do I determine the home directory so the settings are saved and loaded from the proper location?
0 Comments
Accepted Answer
Eric Delgado
on 3 Mar 2023
Edited: Eric Delgado
on 3 Mar 2023
Just create a property named "RootFolder" and put the lines below in the startup of your app.
The property app.RootFolder will keep the name of the root folder of your app no matter it's a standalone version (deployed), a development version (project), or an installed version ("APPS" tab of Matlab).
appName = 'TheNameOfYourApp';
% PATH SEARCH
Flag = 1;
if isdeployed
[~, result] = system('path');
app.RootFolder = char(regexpi(result, 'Path=(.*?);', 'tokens', 'once'));
if ~isfile(fullfile(app.RootFolder, sprintf('%s.exe', appName)))
Flag = 0;
end
else
prjPath = matlab.project.rootProject;
appPath = fullfile(char(com.mathworks.appmanagement.MlappinstallUtil.getAppInstallationFolder), appName);
if ~isempty(prjPath) & strcmp(prjPath.Name, appName)
app.RootFolder = char(prjPath.RootFolder);
elseif isfolder(appPath)
app.RootFolder = appPath;
else
Flag = 0;
end
end
% INITIALIZATION
if Flag
% Call others functions...
else
% Error message...
end
And then you can read your config file easily (don't forget to call it using fullfile!). For example:
app.MyConfigInfo = jsondecode(fileread(fullfile(app.RootFolder, 'Settings', 'MyConfigInfo.json')));
More Answers (0)
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!