MATLAB Answers

# Can I call mex function in other mex functions

20 views (last 30 days)
Chang seok Ma on 20 Feb 2021
Answered: Jan on 20 Feb 2021
Hello,
I am trying to call mex function in other mex functions as below.
I need matrix multiplication but I realized I have to define it before I use it inside mexfunction.
Define 'matrixmultiplication.c' first and then use matrix multiplication inside 'calculation.c'
But I don't think this works.
Is there anyway I can call mexfunction in other mex functions? Or should I make a joint mex function of these two?
Any help would be appreciated
Thanks in advance
1. matrixmultiplication.c (I just downloaded from Matlab example file)
#if !defined(_WIN32)
#define dgemm dgemm_
#endif
#include "mex.h"
#include "blas.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
double *A, *B, *C; /* pointers to input & output matrices*/
size_t m,n,p; /* matrix dimensions */
/* form of op(A) & op(B) to use in matrix multiplication */
char *chn = "N";
/* scalar values to use in dgemm */
double one = 1.0, zero = 0.0;
A = mxGetPr(prhs); /* first input matrix */
B = mxGetPr(prhs); /* second input matrix */
/* dimensions of input matrices */
m = mxGetM(prhs);
p = mxGetN(prhs);
n = mxGetN(prhs);
if (p != mxGetM(prhs)) {
mexErrMsgIdAndTxt("MATLAB:matrixMultiply:matchdims",
"Inner dimensions of matrix multiply do not match.");
}
/* create output matrix C */
plhs = mxCreateDoubleMatrix(m, n, mxREAL);
C = mxGetPr(plhs);
/* Pass arguments to Fortran by reference */
dgemm(chn, chn, &m, &n, &p, &one, A, &m, B, &p, &zero, C, &m);
}
2. calculation.c (Version for practice)
#if !defined(_WIN32)
#define dgemm dgemm_
#endif
#include "mex.h"
#include "blas.h"
void secondfn(double *As, double *Bs, int m, int n, int p, double *Ds, double *Ps){
Ds = matrixmultiplication(As,Bs); // HERE I USED matrixmultiplication.c !!!!!!!
for (size_t i = 0; i < m*n; i++){
Ps[i] = Ds[i];
}
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
double *A, *B, *D, *P; /* pointers to input & output matrices*/
size_t m,n,p; /* matrix dimensions */
A = mxGetPr(prhs); /* first input matrix */
B = mxGetPr(prhs); /* second input matrix */
m = mxGetM(prhs); /* dimensions of input matrices */
p = mxGetN(prhs);
n = mxGetN(prhs);
plhs = mxCreateDoubleMatrix(m, n, mxREAL);
P = mxGetPr(plhs);
D = (double *) mxMalloc(m*n*sizeof(*D));
secondfn(A, B, m, n, p, D, P);
mxFree(D);
}
mex matrixmultiplication.c calculation.c -lmwblas
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

Jan on 20 Feb 2021
The easiest and most efficient solution would be to avoid the overhead of calling a MEX function, but to embed the needed code directly in the other mex function.
But it is possible to call another MEX function by mexCallMATLAB. There is an undocumented function called "mexRunMexFile" also, but as usual for undocumented commands: This is fragile.
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!