Filter löschen
Filter löschen

Directorry for Saving Settings from App Designer

40 Ansichten (letzte 30 Tage)
Mike
Mike am 28 Feb. 2023
Kommentiert: Mike am 6 Mär. 2023
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?

Akzeptierte Antwort

Eric Delgado
Eric Delgado am 3 Mär. 2023
Bearbeitet: Eric Delgado am 3 Mär. 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')));

Weitere Antworten (0)

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by