How to add configuration files to standalone matlab application?

13 ビュー (過去 30 日間)
Sylvain
Sylvain 2025 年 11 月 18 日 12:02
コメント済み: Sylvain 2025 年 11 月 18 日 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 件のコメント
Harald
Harald 2025 年 11 月 18 日 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 2025 年 11 月 18 日 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.

サインインしてコメントする。

採用された回答

Sylvain
Sylvain 2025 年 11 月 18 日 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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

製品


リリース

R2025a

Community Treasure Hunt

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

Start Hunting!

Translated by