Directorry for Saving Settings from App Designer

25 次查看(过去 30 天)
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?

采纳的回答

Eric Delgado
Eric Delgado 2023-3-3
编辑:Eric Delgado 2023-3-3
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')));

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by