Issues using dir() in a .exe file

3 ビュー (過去 30 日間)
Sadie
Sadie 2022 年 7 月 15 日
編集済み: Walter Roberson 2025 年 3 月 20 日
My app (designed in app designer) when the start button is pressed, asks the user to select an input folder, then reads all files from the folder. This is the code that accomplishes that:
inputFolderName = uigetdir();
D = dir(inputFolderName);
When running the app from app designer, this works totally fine. However when packaged to a .exe file, it cannot find any files in the folder. I have tried putting .mat, .png, and a number of other files into the folder to see it is a filetype issue, but it always thinks it is empty.
  2 件のコメント
Sadie
Sadie 2022 年 7 月 15 日
I had the program print a few things to confirm. The filepath it is finding is correct. However, the size of D is 0.
Walter Roberson
Walter Roberson 2022 年 7 月 15 日
I have a vague memory of seeing a bug report along these lines, but I do not recall which MATLAB version.

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

回答 (1 件)

Piyush Dubey
Piyush Dubey 2023 年 6 月 27 日
Hi Sadie,
When an app is packaged to a standalone .EXE file it is possible that the working directory gets modified and that may be the reason why relative paths are not being accessed.
The function ‘fullfile()’ can be used to construct an absolute path from parts of path. Below attached is the documentation to the same:
Hope this helps.
  1 件のコメント
Karansingh Patil
Karansingh Patil 2025 年 3 月 20 日
編集済み: Walter Roberson 2025 年 3 月 20 日
I had the same problem but I found a best way over here and it worked :
if isdeployed
[exePath, ~, ~] = fileparts(which('myscript')); % Replace 'myscript' with your main script name
workingdirectory = exePath;
else
% Use current directory in MATLAB
workingdirectory = pwd;
end
Ideally, isdeployed will check directly if we are running locally or executable so accordingly the paths will be set.
Lets say my folder structrure or all files refered stay where my myscript is - so it will figure that path in executable and set accordingly, where as working directory will set when you run locally.
Works !!!!

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

カテゴリ

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

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by