MATLAB Compiler failed to access specific file
显示 更早的评论
Hi everybody,
I have created an app that loads 2 files (csv and xlsx files) and creates a Word report combining some informations from these files.
Everything is ok even in Matlab and deployed app (via MATLAB Compiler)
BUT
I would like to add a picture (.png) in my Word report. It works when I run it in Matlab but there is a path error when I launch the app deployed with MATLAB Compiler.
I know it's a path error because I use this to copy/paste my picture :
selection.InlineShapes.AddPicture([pwd '/picture.png'],0,1);
I use 'pwd' that is a nonsense for an 'external' app, but I don't know how to orient my path to the ctfroot.
Thanks,
PS. I know that the toolbox Report Generator is the best way to make a report but I can't use it (it's not free).
AL
回答(1 个)
Aashray
2025-2-24
Hi Anne,
Yes, you have rightly pointed out, the “ctfroot” function should be used here to get the correct path for the image.
“ctfroot” returns the root of the compiled application, which is where the resources and files packaged with the app are stored.
Example Code:
imagePath = fullfile(ctfroot, 'picture.png');
selection.InlineShapes.AddPicture(imagePath, 0, 1);
- “ctfroot” gives the root directory of the compiled application.
- “fullfile” ensures that the path is correctly constructed for your operating system (handles forward/backward slashes appropriately).
This allows it to locate “picture.png” no matter where the app is launched.
Also, make sure the .png image file is included when packaging your app. You can either place the image in the same directory as your app's .exe or .app file, or you can use the "Add Files" feature in the MATLAB Compiler to explicitly include it. This ensures that the image is available to the app once it’s deployed.
For example, if you are using mcc to compile, you can use the “-a” flag to add the image file:
mcc -m myApp.m -a picture.png
Attaching documentation links for “ctfroot” and “fullfile” functions for reference:
- https://www.mathworks.com/help/compiler/ctfroot.html
- https://www.mathworks.com/help/matlab/ref/fullfile.html
Hope it helps!
类别
在 帮助中心 和 File Exchange 中查找有关 Debugging and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!