Search code examples
cmatlabmex

Is there a way to use MATLAB function handles in MEX files without the mexCallMATLAB function


I'm currently writing a MEX file in C to speed up MATLAB code that heavily relies on function handles. Unfortunately, my MEX file runs slower than it should because I have to to use mexCallMATLAB to evaluate the handles in MATLAB (as described here).

Most of the function handles are simple inline functions that take uniform random numbers and generate random variables. A typical example is:

f = @(u) exp(norminv(u)) 

Even as the function handles use built-in MATLAB functions, my code has to allow users to use their own function handles, which prevents me from hard coding the functions into a MEX file.

Ideally I would like to provide the MEX file with a way to evaluate this function handle without having to use the mexCallMATLAB function everytime. Does anyone have any idea on how such a thing can be done? I'm open to any suggestions you may have.


Solution

  • As far as I know, that is the only way. A way to help with the speed would be to only call it once - send it a vector of the values that will be needed and use the returned vector in your mex file.