필터 지우기
필터 지우기

mex calls in other mex functions - is it possible?

조회 수: 3 (최근 30일)
Rajaram B S R
Rajaram B S R 2011년 11월 9일
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

채택된 답변

Titus Edelhofer
Titus Edelhofer 2011년 11월 9일
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
Jan
Jan 2011년 11월 9일
@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 2011년 11월 9일
@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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Friedrich
Friedrich 2011년 11월 9일
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
Titus Edelhofer
Titus Edelhofer 2011년 11월 9일
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 2011년 11월 9일
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).

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by