0

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?

Phil Goddard
  • 10,571
  • 1
  • 16
  • 28
guilhermecgs
  • 2,913
  • 11
  • 39
  • 69
  • 1
    Why would you do that? why not use the mexfunction as wrappers, that call real functions inside? If you'd do it this way you wouldn't have the problem, because you'd call the real function, not something that is designed as a a MATLAB-C interface. – Ander Biguri Sep 28 '15 at 14:43
  • That is an pertinent questions. It is somehow hard to explain why, but the simple answer is: Both functions access some data stored in memory. Because of that, they need to access the SAME data in order to produce the same results. – guilhermecgs Sep 28 '15 at 15:43
  • 1
    If you would have a single function that does stuff that would not change. My point: instead of having something like `mexA-call>mexB` do `fun` where `mexA->fun` and `mexB->fun`. Problem solved – Ander Biguri Sep 28 '15 at 16:02
  • 1
    @AnderBiguri : Everything you said is correct, but due to specific implementation and requirements of my functions, calling mexA->fun and mexB->fun would allocate 2 different memmory blocks and thus, different results. I should have made this behaviour clear in my question to avoid confusion. I´m sorry. – guilhermecgs Sep 28 '15 at 16:13

1 Answers1

2

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.

Phil Goddard
  • 10,571
  • 1
  • 16
  • 28