Compiling Matlab shared library with image processing toolbox - matlab

I'm trying to compile C shared library from Matlab. My Matlab code uses a lot of the image processing functionality. So, compiling goes fine, but when I call the dll from my application, I get messages like:
"Undefined function or method 'XYZ' for input arguments of type double".
I have verified my arguments are ok -- it's not a type problem. So, I tried adding %#function XYZ to my .m file, but that didn't help anything. Then, I tried using the -a flag in my compile command:
eval(['mcc -v -N -W lib:cshared -d ' clibdir ' -T link:lib -a edge' allFiles]);
but it fails to compile with:
Depfun error: 'Unable to locate edge as a function on the MATLAB path'
I have verified the image processing files are on my computer (I can run everything from matlab with no problem) and my path points to the directory that contains them.
I've also tried copying the toolbox .m files into my working directory, but that quickly balloons into a lot of files. And, for some functions, there is no .m - just a .mex - and i haven't found a way to include a mex file into my .dll.
What am I missing?

Have you tried including the Image Processing Toolbox folder using the -a option? For example:
mcc ... -a C:\Program Files\MATLAB\R2009a\toolbox\images\images
According to the mcc documentation, all files in this folder, as well as all files in any subfolders, are added to the CTF archive, and the folder subtree is preserved in the CTF archive.
If you don't want to include every subfolder, you can load only the files in a folder using a wildcard pattern:
mcc ... -a C:\Program Files\MATLAB\R2009a\toolbox\images\images\*
This may be necessary if there is a subfolder that may have functions or scripts that could shadow ones in the parent folder. For example, there is an edge.m function in the parent folder C:\Program Files\MATLAB\R2009a\toolbox\images\images\, and there is a ja subfolder that contains Japanese language help files (on Windows), one of which is also called edge.m. You wouldn't want this subfolder to be added when compiling, so you could either:
Remove that subfolder temporarily, add the parent folder without the wildcard option (to add the other subfolders you do want), then put that folder back.
Add the parent folder with the wildcard option (to add just the files), then separately add only the subfolders you want (such as #strel and private) with an additional -a command. NOTE: I'm uncertain if adding subfolders separately will maintain the folder subtree of the parent directory in the CTF archive in the same way as option #1 would!
If you don't want to include a large list of files that may not end up being used, you could instead try using the function DEPFUN to first get a list of dependencies for your MATLAB code. Then from this list you can find the specific Image Processing Toolbox functions your code uses and include only those when compiling. Since you specifically asked, this newsgroup thread mentions how to include a .mex file:
mcc ... -a imreconstructmex.mexw32 %# For a 32-bit Windows mex file
NOTE: There is also a MathWorks bug report I came across (which you need a login to see) that mentions a problem compiling applications using some Image Processing Toolbox functions on Windows in R2009b. There is a workaround given at the above link. This bug is fixed as of R2010a.

Related

'Current Directory' search path for INCLUDE in GNU linker (ld)?

I have broken out the MEMORY{} command of my linker script into a separate file memmap.ld in order to share it between projects (bootloader and target firmware).
The file is currently located in the same directory as the main linker script and I include it using the line
INCLUDE memmap.ld
from my main .ld script however this does not work and I get the error
ld.exe: cannot open linker script file memmap.ld: No such file or directory
when building. The section of the ld manual dealing with the INCLUDE filename command reads
Include the linker script filename at this point. The file will be searched for in
the current directory, and in any directory specified with the ‘-L’ option. You
can nest calls to INCLUDE up to 10 levels deep.
You can place INCLUDE directives at the top level, in MEMORY or SECTIONS commands, or in output section descriptions.
What is up with this? Is the current directory taken from the current script, executable, some other external parameter?
Am using the ST build of the GNU Linker distributed with the 64 bit Windows STM32CubeIDE if it makes any difference.
It appears the matching of .ld scripts within the current directory when using INCLUDE only compares with script names introduced via either '-L searchdir' or '-T scriptfile'.
[i.e. the LD program doesn't do a file system search but instead searches the list of scripts provided during invocation (or at least behaves as such) ].
My case in point when using STM32CubeIDE only provides the option to provide a single loader script via the -T option within project system (while Eclipse Embedded CDT which ST bases it's tools on allows multiple files to be passed by -T) required the addition of the folder containing both my scripts using the -L option (from the Libraries subsection of the Linker project properties).
Once the script I was referencing inside LD was being passed through the build system by including the directory location in the command line the 'current directory' did resolve to that of the script doing the inclusion.

add path to subfolder containing dependeble .m files that are not bundled in the compilation

I am trying to deploy an application which are dependent on specific data input files in the form of .m files.
I know how to bundle all of the files "freezing" them within the compilation: like so mcc -m file.m -a path_to_data*.m.
However, I was wondering if it is possible to just add the path to the data folder into the deployment project and then have the data files within the folder being interchangeable (i.e. being added to the MATLAB path upon execution and not freezed during compilation). The advantage of this would be that the application could still be used if some data files needed to be changed without having to recompile everything.
What would be the best way to do this? Is it possible with the MATLAB compiler?
ps. I am working on a unix system

Including multiple folders (with images, scripts, etc) within a Matlab standalone GUI.exe

I have a software that has multiple GUIs. To organize things better (or at least that was my thought), I have created several folders within the root directory as it can be seen in this image.
Within the folders i have both files with different formats and also some Matlab scripts.
When creating the Matlab executable using the Application compiler, and after selecting the main file, Matlab does not directly detected that these same folders are important for the code to run. Therefore I decided to add the folders manually.
Once the setup is created and installed, by running the application within the Matlab environment, I was able to debug one possible issue why the software is not running.
As you can see in the first image, the "play.png" is within the Images folder.
My question is pretty straight forward: how to force the Matlab Compiler to learn that all these folders are to be included in the setup? Not only to be included but their paths'
Two things could be going on:
You are not including the files in the package.
Make sure that you include them using the -a option of mcc:
mcc -m hello.m -a ./testdir/*
You can also use the GUI, of course, see here.
You are looking for the included files in the wrong place. Use ctfroot as the root of all paths in your code:
img_file_name = fullfile(ctfroot,'Images','brain.jpg'));
Check the unpacked CTF file (it is automatically unpacked when executed) to see the directory structure in it. ctfroot points to the root of the unpacked CTF file.
PS: This blog post might give you some more pointers.

remove curve fitting toolbox for packaging

I have a matlab guide program I want to compile (package).
This program is not using the curve fitting toolbox.
Nevertheless the compiler complains about a missing licence.
mcc -C -o ...
Compiler version: 5.0 (R2013b)
Dependency analysis by DEPFUN.
Processing C:\Program Files\MATLAB\R2013b\toolbox\matlab\win64\mcc.enc
Processing C:\Program Files\MATLAB\R2013b\toolbox\curvefit\win64\mcc.enc
Depfun error: 'License checkout failed.
License Manager Error -4
Maximum number of users for Curve_Fitting_Toolbox reached.
How can I remove this dependancy for compilation?
According to the mcc document,
-N Clear Path
Passing -N effectively clears the path of all folders except the following core folders
(this list is subject to change over time):
matlabroot\toolbox\matlab
matlabroot\toolbox\local
matlabroot\toolbox\compiler\deploy
It also retains all subfolders of the above list that appear on the MATLAB path at compile time.
Including -N on the command line lets you replace folders from the original path,
while retaining the relative ordering of the included folders.
All subfolders of the included folders that appear on the original path are also included.
In addition, the -N option retains all folders that you included on the path that are not under matlabroot\toolbox.
In other words, you can clear all folders from the path and only keep the core MATLAB ones.
If you want to include some toolbox folders, you can use the -a option:
mcc ... -a C:\Program Files\MATLAB\R2013b\toolbox\...\...
Personally, I simply temporarily rename toolbox folders I don't need.
Just manually navigate to "matlabroot()\toolbox\" and rename, say, "signal" -> "signal_".
Surprisingly, the same could be done from the script, calling mcc() - almost no files in those folders are locked by matlab.exe.
This method doesn't sound like a "solution", of course it's just a "quick-and-dirty workaround". But, for me, it seem to work (R2015a).
Note, that you can easily check, what's actually included in your "deployed" dll (or exe), with 7z. Including files, added from toolboxes - see "my.dll.text\toolbox\"
PS1:
From my experience ,"-N" removes some unrelated stuff. Not all.
Moreover, when using "-N", you should carefully add stuff you need with "-I".
PS2:
In ".prj" file, created by deploytool(), there's something, that sounds related:
<matlab>
<root>D:\Program Files\MATLAB\MATLAB Production Server\R2015a</root>
<toolboxes>
<toolbox name="matlabcoder" />
...
</toolboxes>
...
</matlab>
but I've not checked, whether this actually controls witch toolboxes are used by mcc.

MATLAB compiler processes mcc.enc from unrelated toolboxes

I'm using mcc to compile my MATLAB project. The machine I'm using has many MATLAB toolboxes installed on it, but my code only requires a few (such as distcomp and images). However, whenever I run mcc, it ends up "processing" the mcc.enc files for many toolboxes, including those that my code does not need.
Here is the beginning of the output from mcc (I'm running it with -R '-nodisplay' to be compatible with the cluster I'm using).
Warning: No display specified. You will not be able to display graphics on the screen.
Compiler version: 4.15 (R2011a)
Processing /cm/shared/apps/matlab/7.12.0/toolbox/matlab/mcc.enc
Processing /cm/shared/apps/matlab/7.12.0/toolbox/distcomp/mcc.enc
Processing /cm/shared/apps/matlab/7.12.0/toolbox/images/mcc.enc
Processing /cm/shared/apps/matlab/7.12.0/toolbox/stats/mcc.enc
Processing /cm/shared/apps/matlab/7.12.0/toolbox/shared/imageslib/mcc.enc
Processing /cm/shared/apps/matlab/7.12.0/toolbox/shared/system/mcc.enc
Processing /cm/shared/apps/matlab/7.12.0/toolbox/shared/spcuilib/mcc.enc
Processing /cm/shared/apps/matlab/7.12.0/toolbox/control/mcc.enc
Processing /cm/shared/apps/matlab/7.12.0/toolbox/shared/controllib/engine/mcc.enc
Processing /cm/shared/apps/matlab/7.12.0/toolbox/shared/controllib/general/mcc.enc
Processing /cm/shared/apps/matlab/7.12.0/toolbox/signal/mcc.enc
Processing /cm/shared/apps/matlab/7.12.0/toolbox/nnet/mcc.enc
I've noticed it does the same thing when running mcc on my desktop as well. Some of these mcc.enc files (especially .../signal/mcc.enc and .../nnet/mcc.enc), take more time to process than it takes the rest of mcc to execute. Is there any way to exclude processing unneeded mcc.enc files?
Using the "deplyment tool" in the MATLAB IDE, go the project settings and from the "toolboxes on path" tab, unselect all toolboxes you do not need.
Also, there is an mcc argument -N that clears all folders from the path and only keep the core MATLAB ones.
-N Clear Path
Passing -N effectively clears the path of all folders except the
following core folders (this list is subject to change over time):
matlabroot/toolbox/matlab
matlabroot/toolbox/local
matlabroot/toolbox/compiler/deploy
It also retains all subfolders of the above list that appear on the
MATLAB path at compile time. Including -N on the command line allows
you to replace folders from the original path, while retaining the
relative ordering of the included folders. All subfolders of the
included folders that appear on the original path are also included.
In addition, the -N option retains all folders that the user has
included on the path that are not under matlabroot/toolbox.
I would recommend using the «fdep» function from the Matlab Central File Exchange:
http://www.mathworks.com/matlabcentral/fileexchange/17291-fdep--a-pedestrian-function-dependencies-finder
This function, applied to the main function of your program will tell you the toolboxes you are using and all the functions that are being used (recursively, so all the elemental functions are listed and you shouldn't miss any function). It is really useful to reduce the size and the compiling time of your final exe.