mex calls in other mex functions - is it possible?

3 views (last 30 days)
Hello,
Is it possible to have mex-function calls in other mex-functions? if yes, how do I go about it?
I tried to have 2 mex functions f1.c and f2.c; The function f1 has the data initialized and its fed to f2 for addition. But compiling f1 is having issues with the f2 call (unresolved external symbol _f2 in mexFunction - error) as there is no prototype available.
How do I make the declaration for the same?
I know I need to have a header file included with the prototype but I need to know how to write the prototype for the mex-function.
Thanks in advance

Accepted Answer

Titus Edelhofer
Titus Edelhofer on 9 Nov 2011
Hi,
if the other function is really a mex function, you will need to use the function mexCallMATLAB, which will call then again into the other mex function ...
Or, you rename the function mexFunction in your f2.c and make it a "usual" dll, then you would not need to go through MATLAB.
Titus
  3 Comments
Jan
Jan on 9 Nov 2011
@Rajaram: The Mex function *is* a Matlab function. The standard method is to call Matlab through mexCallMATLAB, such the Matlab calls the new Mex again. This is not efficient. If you have to do this repeatedly, think of joining the Mex-files.
Does anybody knows how to use "mexRunMexFile"?
Titus Edelhofer
Titus Edelhofer on 9 Nov 2011
@Rajaram: regarding mexCallMATLAB, see post(s) of Jan.
Regarding "usual" dll: if you rename the mexFunction, use your C compiler to generate a dll (not a mex dll). If you happen to have the compiler, take a look at the script mbuild, that makes dll compiling C code into a dll fairly easy.
Titus

Sign in to comment.

More Answers (1)

Friedrich
Friedrich on 9 Nov 2011
Hi,
when working on Windows you can use the Windows API to do run time dynamic linking on the mex file since a mex file is a dll with a different ending which exports the function "mexFunction". So you could use loadlibrary and GetProcAdress function to call the mex file. An example is given on a Microsoft page:
So based on the example above I made a smal test which works fine.
1.) create a tbc.c file which contains:
#include "mex.h"
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[] )
{
int i = 0;
mexPrintf("number of inputs %d \n",nrhs);
for(i=0;i<nrhs;i++){
mexPrintf("Value of input %d is %f \n",i,*mxGetPr(prhs[i]));
}
for(i=0;i<nlhs;i++){
char buffer[50];
sprintf(buffer,"here is your output number %d",i);
plhs[i] = mxCreateString(buffer);
}
}
2.) create a caller.c file:
#include "mex.h"
#include <windows.h>
#include <stdio.h>
//based on
//http://msdn.microsoft.com/en-us/library/windows/desktop/ms686944%28v=vs.85%29.aspx
//call it form MATLAB like
//[a,b,c] = caller(3,4,5)
typedef int (__cdecl *MYPROC)(int, mxArray*[], int, const mxArray*[]);
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[] )
{
HINSTANCE hinstLib;
MYPROC ProcAdd;
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
// Get a handle to the DLL module.
hinstLib = LoadLibrary(TEXT("tbc.mexw64"));
// If the handle is valid, try to get the function address.
if (hinstLib != NULL)
{
ProcAdd = (MYPROC) GetProcAddress(hinstLib, "mexFunction");
// If the function address is valid, call the function.
if (NULL != ProcAdd)
{
fRunTimeLinkSuccess = TRUE;
(ProcAdd) (nlhs,plhs,nrhs,prhs);
}
// Free the DLL module.
fFreeResult = FreeLibrary(hinstLib);
}
// If unable to call the DLL function, use an alternative.
if (! fRunTimeLinkSuccess)
printf("unable to call the other MEX file\n");
}
3.) compile both
4.) run caller from MATLAB and you will see some output is displayed in ML,
[a,b,c] = caller(3,4,5)
number of inputs 3
Value of input 0 is 3.000000
Value of input 1 is 4.000000
Value of input 2 is 5.000000
a =
here is your output number 0
b =
here is your output number 1
c =
here is your output number 2
  2 Comments
Titus Edelhofer
Titus Edelhofer on 9 Nov 2011
Hi Friedrich,
nice! Two comments: on a 32 Bit machine you need to load of course tbc.mexw32. Second, it looks as though LCC on 32Bit can not compile this but you will need the Microsoft compiler...?
Titus
Friedrich
Friedrich on 9 Nov 2011
Hi Titus,
yes you are right with both statements. The filename must be correct in the LoadLibrary command (ending depends on 32bit or 64bit). And for the Windows API one need a C++ Compiler which one can get for free (Express version).

Sign in to comment.

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!