Search code examples
c++matlabsimulinkmexs-function

Call mex Function from cmex SFunction


I have implemented a complex mexFunction using visual studio 2012 and successfully integrated it with Matlab. (lets call it mexFunctionA.mexw32 )

When I run this command in matlab command window, I get the expected results:

mexFunctionA("My1Argument", "My2Argument");

Now , I need to develop a mexFunctionB that calls mexFunctionA; mexFunctionB is a simple as it can be.

The C code I´m trying (inside mexFunctionB.c) is:

#include "mexFunctionA.mexw32"

(...)

static void mdlOutputs(SimStruct *S, int_T tid)
{
    mexFunctionA("My1Argument", "My2Argument");
}

(...)

This line of code is not compiling.

The command line I am using is:

mex -v mexFunctionB.c -I'C:\patchToMexFunctionA' -L'C:\patchToMexFunctionA' 'mexFunctionA.mexw32'

So, here are the possible errors:

  1. The #include method is wrong.
  2. The command line for compiling the code is wrong.
  3. It is not possible to do what I am planning to do.
  4. Something else.

Anyone knows how to fix it?


Solution

  • The code you give is non-sensical. .mexw32 files are dynamically linked libraries (i.e. dll's), and in C code #include statements aren't used to include dll's.

    Firstly note that as far as your S-Function is concerned mexFunctionA is no different from any other MATLAB function. So the question you should be asking is "how do I call a MATLAB function from within a mex file?".

    The answer to that is to use the function mexCallMATLAB.

    In short, you need to remove the #include and reformat the call to mexFunctionA to the form required by mexCallMATLAB.