How to add configuration files to standalone matlab application?

13 Ansichten (letzte 30 Tage)
Sylvain
Sylvain am 18 Nov. 2025 um 12:02
Kommentiert: Sylvain am 18 Nov. 2025 um 14:48
I am stuck at developping my standalone application.
I have a user data file that is called "preferences.toml" it is a typical config file, that is being modified by the user. It cannot be stored on C:\Program Files applications for obvious reasons that it will be modified by the user.
The file will be stored in the APDATA/Roaming/Author Name/App Name, the folder is automatically detected or created. But now I want to copy/paste a default config file.
I attached the file/folder linck to the user_data
But at compilation, the folder is not created
  2 Kommentare
Harald
Harald am 18 Nov. 2025 um 13:16
Hi,
are you getting any error messages? To ensure that you observe any error messages, start the executable from a system console rather than by double-clicking it.
Another problem may be that APPDATA/Roaming/Author Name/App Name may not exist on the end user's machines. You can use getenv("USERNAME") to obtain the login name.
If the file is temporary, it might also be an alternative to store it in the folder returned by tempdir.
If the above does not help, it would be great if you can provide a minimal reproduction example.
Best wishes,
Harald
Sylvain
Sylvain am 18 Nov. 2025 um 14:48
Please see my solution, All the files are compiled and put into an archive that is accessible using ctfroot (instead of pwd). You have to twick what is coming after, to select the correct path.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Sylvain
Sylvain am 18 Nov. 2025 um 14:45
I finally got around my question.
In summary:
  1. add the files in the compiling option (this is what I did)
  2. Compile
  3. Install
  4. The files are present in the matlab runtime folder: ctfroot
  5. Below a code snippet to copy paste
function userDataDir= get_userAppDir(app_author,app_name)
if isdeployed
% return the path to the "APPDATA/Roaming"
if ispc
baseDataDir = getenv("APPDATA"); % e.g. C:\Users\...\AppData\Roaming
userDataDir = fullfile(baseDataDir, app_author, app_name);
elseif ismac
baseDataDir = fullfile(getenv("HOME"), "Library", "Application Support");
userDataDir = fullfile(baseDataDir, app_name);
else
% Linux and UNIX-like
baseDataDir = fullfile(getenv("HOME"), ".local", "share");
userDataDir = fullfile(baseDataDir, app_name);
end
else
userDataDir = fullfile(pwd,"user_data/");
end
end
function set_userAppDir(app_author,app_name)
% prepare the user data environement
if ~isdeployed
return
else
try
% step 1: retrieve path
userDataDir= get_userAppDir(app_author,app_name);
% Ensure directory exists
if ~exist(userDataDir, 'dir')
uimsgbox(title,msg);
mkdir(userDataDir);
end
% step 2: retrieve the archive:
defaultPrefsFile = fullfile(ctfroot,app_name, "user_data",'preferences.toml');
% step 3: copy the default file
if ~exist(defaultPrefsFile,"file")
%error handling
else %copy files
prefsFile = fullfile(userDataDir, 'preferences.toml');
copyfile(defaultPrefsFile, prefsFile);
end
catch ME
%error handling
uimsgbox("error",ME.message)
end
end
end
This was relatively laborious to debug in compiled mode. for error handling I am using the following helper (which is not perfect, maybe Mathworks has a more efficient way to display the exceptions in compiled environment.
function uimsgbox(title,msg)
% custom uialert with close function
fig = uifigure('Name', 'Warning','WindowStyle', 'modal');
% Display alert
uialert(fig, msg, title, 'Icon', 'warning','CloseFcn', @(~, ~)close(fig));
uiwait(fig);
end

Weitere Antworten (0)

Kategorien

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

Produkte


Version

R2025a

Community Treasure Hunt

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

Start Hunting!

Translated by