Directorry for Saving Settings from App Designer

26 visualizaciones (últimos 30 días)
Mike
Mike el 28 de Feb. de 2023
Comentada: Mike el 6 de Mzo. de 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?

Respuesta aceptada

Eric Delgado
Eric Delgado el 3 de Mzo. de 2023
Editada: Eric Delgado el 3 de Mzo. de 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')));

Más respuestas (0)

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by