Generate C code from matlab code issue
Show older comments
why below uigetfile can't be generate C code? How can I do it?
[file,path]=uigetfile("*.png");
Answers (2)
Walter Roberson
on 11 Feb 2025
0 votes
Code cannot be generated for uigetdir because the C standard library does not define any directory operations.
Directory operations are defined by Posix, but MATLAB Coder does not target Posix.
You can use coder.ceval to call Posix directory operations such as opendir() and related functions. You are then stuck presenting the information in some kind of graphical user interface. MATLAB Coder has no built-in support for graphics.
4 Comments
Walter Roberson
on 11 Feb 2025
Create C code that includes dirent.h and has a single entry point. Have the C code call opendir() https://pubs.opengroup.org/onlinepubs/009604599/functions/opendir.html and have it loop calling readdir() https://pubs.opengroup.org/onlinepubs/009604599/functions/readdir.html and finally call closedir() https://pubs.opengroup.org/onlinepubs/009604599/functions/closedir.html
readdir() returns information about file names. If you need more information about the file then call stat() https://pubs.opengroup.org/onlinepubs/009604599/functions/stat.html
At this point, you have to have some kind of call to GUI code to present the list of files to the user and have the user select one of them. There are a wide variety of graphic systems, mostly incompatible, so I cannot suggest much.
There are alternatives if you are willing to commit to particular operating systems. For example Windowss https://cboard.cprogramming.com/c-programming/136029-openfiledialog-window-c.html
Now, no matter your implementation, you are going to need to return information about the file name. It is generally an error to allocate memory in C code and return the memory to MATLAB. For this reason it is typically better to allocate a buffer in MATLAB and pass the address of the buffer into C and write the resulting file name into the buffer. The buffer needs to be sized according to the operating system -- for example 260 for Windows, 1024 for MacOS and 4096 for Linux.
xie
on 13 Feb 2025
Walter Roberson
on 13 Feb 2025
Raghu Boggavarapu
on 12 Feb 2025
0 votes
We do not support MATLAB Graphics functionality for code generation. If you are primarily interested in getting list of files, you may consider using MATLAB system function and call appropriate command like "ls" to fetch the list of files. We support code generation for system function starting R2024b.
If this does not solve your use case, please share more details here Request Code Generation Support for MATLAB Functions - MATLAB & Simulink
Categories
Find more on Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!