Location of mex executable

13 views (last 30 days)
Anthony Barone
Anthony Barone on 22 Apr 2016
Answered: Povl Abrahamsen on 13 Aug 2021
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.

Answers (1)

Povl Abrahamsen
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:
  1. Call "which" with mexFunctionName() as the argument
  2. Call "filesep"
  3. Truncate the result of the "which" at the last occurrance of "filesep"
#include "mex.h"
#include "gamma_functions.h"
#include <string.h>
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
mxArray *rhs[1], *lhs[1], *lhs2[1];
char *path_to_mexfile, *end_of_path, *filesep;
int pathlen;
mexCallMATLAB(1, lhs, 1, rhs, "which");
pathlen = mxGetNumberOfElements(lhs[0]) + 1;
path_to_mexfile = mxCalloc(pathlen, sizeof(char));
mxGetString(lhs[0], path_to_mexfile, pathlen);
mexCallMATLAB(1, lhs2, 0, NULL, "filesep");
filesep=(char *)mxGetChars(lhs2[0]);
end_of_path[1]=0; /* terminate path after last filesep */
/* path_to_mexfile now contains the path to the mex file,
with a trailing file separator */

Community Treasure Hunt

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

Start Hunting!

Translated by