How to get file names in a folder, when using C/C++ Code Generation

4 views (last 30 days)
Hi! My code needs to get all bmp image file names in a folder , and convert it to C++ dll by Matlab Coder. Unfortunately, Matlab Coder is not support the dir function . I want to find a replacement method to get all bmp file names in a folder and also can be used in Matlab Coder.
Thanks a lot!

Answers (1)

Jinal
Jinal on 18 Oct 2023
Edited: Jinal on 20 Oct 2023
Hello Guan,
As per my understanding, you want to generate C/C++ code from your MATLAB code that lists the names of all BMP files present in a folder.
At present, functions such as “dir” and “ls” are not supported by MATLAB Coder for C/C++ code generation.
Any of the following workarounds can be used to include functions not supported by MATLAB Coder:
1) Use “coder.extrinsic” to call the unsupported functions. “coder.extrinsic” declares function as an extrinsic function. The code generator uses the MATLAB® engine to execute the call.
Please find below a sample workflow to list BMP files present in a folder:
  • Create a local function called “bmplist” as follows.
Create a local function called “bmplist” as follows.
function fn = bmplist(folderpath)
coder.extrinsic("dir");
coder.extrinsic("cd");
cd(folderpath);
fn = dir('**/*.bmp');
end
  • Generate MEX code for “bmplist” by typing the following command.
codegen bmplist -args {"hi"}
  • Call the generated MEX code.
bmplist(folderpath);
%folderpath is the path to the folder in which BMP files are to be searched for
To know more about “coder.extrinsic”, please refer this link:
2) Write custom code for the unsupported functions.
3) Use “coder.ceval” to call custom C functions that replace the unsupported functions.
To know more about “coder.ceval”, please refer the following link:
Please refer the following link to know more about workarounds for using functions not supported for Code Generation:
I hope this resolves the issue you were facing.

Community Treasure Hunt

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

Start Hunting!