- Call "which" with mexFunctionName() as the argument
- Call "filesep"
- Truncate the result of the "which" at the last occurrance of "filesep"
Location of mex executable
13 views (last 30 days)
I am trying to find the location of the mex executable for use with another program (madagascar). In order to use madagascar, it needs to generate sone mex files from some c source code, so it needs to know the file location of the mex executable. I can compile and execute mex files in matlab, but i am unable to find the exact location of the mex executable so that I can link this external program to it (i have tried many different possible locations without success). Unfortunately I cannot manually generate the mex files that madagascar needs through matlab since it uses a non-standard c library file (rsf.h). If anyone can help me locate the exact file path for the mex executable that would be great.
I am using matlab 2016a and am using minGW-w64 4.9.2 as the compiler.
Povl Abrahamsen on 13 Aug 2021
I realize it's five years since this question was asked, but I've just had the same issue, and have come up with the following solution:
void mexFunction(int nlhs, mxArray *plhs,
int nrhs, const mxArray *prhs)
mxArray *rhs, *lhs, *lhs2;
char *path_to_mexfile, *end_of_path, *filesep;
mexCallMATLAB(1, lhs, 1, rhs, "which");
pathlen = mxGetNumberOfElements(lhs) + 1;
path_to_mexfile = mxCalloc(pathlen, sizeof(char));
mxGetString(lhs, path_to_mexfile, pathlen);
mexCallMATLAB(1, lhs2, 0, NULL, "filesep");
end_of_path=0; /* terminate path after last filesep */
/* path_to_mexfile now contains the path to the mex file,
with a trailing file separator */