Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: how to pass a function from Matlab to mex file
Date: Mon, 1 Dec 2008 21:42:01 +0000 (UTC)
Organization: PhysioSonics Inc
Lines: 31
Message-ID: <gh1ln9$son$1@fred.mathworks.com>
References: <g7efi9$o2$1@fred.mathworks.com> <g7jcsl$qs0$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1228167721 29463 172.30.248.38 (1 Dec 2008 21:42:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 1 Dec 2008 21:42:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1129061
Xref: news.mathworks.com comp.soft-sys.matlab:504231


"James Tursa" <aclassyguywithaknotac@hotmail.com> wrote in message <g7jcsl$qs0$1@fred.mathworks.com>...
> Having said that, you *can* call MATLAB functions from
> within C code using the mexCallMATLAB function. You will
> need to first convert the passed in function_handle to a
> character string, and then convert any C arrays into
> mxArrays, but it can be done. I have shown a simple example
> below.

I have found that it is also possible to use function handles by calling feval in mexCallMATLAB, which avoids the need to convert the function_handle to a character string.  The mex function is:

/* mexFEval */

#include "mex.h"
   
void
mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
{
    /* Evaluate string in MATLAB workspace */
    if ( mxIsClass(prhs[0], "function_handle") )
        mexCallMATLAB(nlhs, plhs, nrhs, prhs, "feval");
    else
        mexErrMsgTxt("First Input is not a function handle.");            
    
    return;    
    
}

Then I can call it like this:

[outArg1, outArg2, ...] = mexEvalFunctionHandle(function_handle, inArg1, inArg2)