Question

I know it could souds as a very famous topic, but I didn't find anything that helps me with this.

This is the scenario:

  1. I have a myFun.m function, no matter what's inside, it is a generic function which could potentially depends on other toolboxes. It has inputs and outputs;

  2. I want to generate the analogous myFun.mex function;

I have Matlab Compiler and I can compile myFun.m as

mcc -v -W lib:libmyFun -T link:lib myFun

In this way I get some new files:

libmyfun.c

libmyfun.dll

libmyfun.exp

libmyfun.exports

libmyfun.h

libmyfun.lib

Reading around and following Loren's example I guess I have to create a myfun.cpp which inside includes the very famous gateway function:

/* The gateway function */
void mexFunction(int nlhs, mxArray *plhs[],
                 int nrhs, const mxArray *prhs[])
{
/* variable declarations here */

/* code here */
}

I've tried to do that and called the mex generation routine:

mex('-v', 'myfun.cpp')

but I get several errors, and I must say my myfun.cpp is copy/paste function where I have a lot of doubts on... especially on inputs/outputs management.

My question is... apart from the myfun.cpp function which could be full of errors as I'm not a C/C++ developer... is the process correct? If the answer is YES, does anybody knows which is the generic way to write the myfun.cpp?

Cheers,

Sebastian

Was it helpful?

Solution

MATLAB Compiler does not do what you want.

MATLAB Compiler takes your .m code and encrypts and archives it into a .ctf (Component Technology File) file. It then produces a thin wrapper (either a .exe file, or a .dll library file along with files to enable calling the library from C). You deliver the .ctf file and the wrapper to your end user along with the freely redistributable MCR (MATLAB Compiler Runtime). It's possible to package either the first two or all three into a single unit for easier distribution.

The end user runs the executable or library, which dearchives and decrypts the .m code and runs it against the MCR rather than MATLAB. You can think of the MCR as basically a copy of MATLAB, but without the front-end desktop environment.

MATLAB Compiler is intended for the use case that you want to easily share a MATLAB application or algorithm with someone who does not have MATLAB. Because the code is encrypted, you can also use it to protect your intellectual property. But the code remains as .m code, and executes exactly as it would within MATLAB, including the same speed.

MEX is something different entirely. If you have an algorithm implemented in C code, you can add the 'gateway' routine you mention to it, and compile it with the command mex into a library that is then callable from MATLAB as if it were a regular MATLAB command or function. MEX functionality is part of regular MATLAB, and doesn't require any add-on products.

There is also another product, MATLAB Coder, that is distinct from MATLAB Compiler.

MATLAB Coder takes .m code that is in a subset of the MATLAB language, and converts it to C code. The subset is very extensive, but there are a few significant restrictions on the parts of the MATLAB language that are supported. I'm afraid those restrictions include many toolbox functions, including some functionality from Neural Networks Toolbox.

You can then do lots of things with that C code, including using MEX to compile it back into a form usable as a MATLAB command. This can often, though not always, provide a significant speedup over the original .m code. You can also do other things such as integrate the C code into a wider C project. or deliver it to an embedded device.

The main answer to your question is:

  1. MATLAB Compiler doesn't do what you need.
  2. To produce a MEX file file from your .m code, either recode it manually into C and then mex it, or use MATLAB Coder to automatically produce C code and then mex it.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top