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:
- Call "which" with mexFunctionName() as the argument
- Call "filesep"
- 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;
rhs[0]=mxCreateString(mexFunctionName());
mexCallMATLAB(1, lhs, 1, rhs, "which");
mxDestroyArray(rhs[0]);
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=strrchr(path_to_mexfile,filesep[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 */
}