Directorry for Saving Settings from App Designer

조회 수: 28 (최근 30일)
Mike
Mike 2023년 2월 28일
댓글: Mike 2023년 3월 6일
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