How to add configuration files to standalone matlab application?

13 views (last 30 days)
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 Comments
Harald
Harald on 18 Nov 2025 at 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 on 18 Nov 2025 at 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.

Sign in to comment.

Accepted Answer

Sylvain
Sylvain on 18 Nov 2025 at 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

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2025a

Community Treasure Hunt

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

Start Hunting!