How can I get the full path of my current *.mlapp file?

50 ビュー (過去 30 日間)
Mark Golberg
Mark Golberg 2022 年 6 月 15 日
コメント済み: Mark Golberg 2022 年 6 月 19 日
Hi
I'm building a gui via App Designer. I'd like to specify a path of an XML file. I'd like it to be relative to my main *.mlapp file.
Eventually, I'd like to ha ve a general function of the following format:
IF running in StandAlone Mode --> XML file path should be relative to where the exe file recides (when distributed, I'd ask the users to keep this XML file in the same folder as the exe file).
IF running in Developers Mode --> XML file path should be relative to when the *.mlapp file recides.
THANKS!

採用された回答

Mark Golberg
Mark Golberg 2022 年 6 月 19 日
if isdeployed
% User is running an executable in standalone mode.
[~ , result] = system('set PATH');
executableFolder = char(regexpi(result, 'Path=(.*?);', 'tokens', 'once'));
else
% User is running an m-file from the MATLAB integrated development environment (regular MATLAB).
tmp_path = mfilename('fullpath');
[executableFolder , ~ , ~] = fileparts(tmp_path);
end
  1 件のコメント
Mark Golberg
Mark Golberg 2022 年 6 月 19 日
I use the above code in a function, and then the function m-file location in the same folder as the *.mlapp file.

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

その他の回答 (1 件)

Fangjun Jiang
Fangjun Jiang 2022 年 6 月 16 日
I think you can implement this in your code.
Search for the .exe file or .mlapp file using exist('', 'file'), use which() and fileparts() to get the root directory. If both exists, pick your priority.
Then, use fullfile() to combine the root directory and folder to specify the full path of the .xml file.

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by