From: James Tursa on
"Etienne" <etienne.coetzee(a)airbus.com> wrote in message <hluga1$jl5$1(a)fred.mathworks.com>...
> "Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <hloubj$b12$1(a)fred.mathworks.com>...
> > Etienne, if you need to call a function handle from MEX, use mexCallMATLAB with FEVAL.
> >
> > Bruno
>
> Hi Bruno
>
> I have tried the code below, but the syntax I am using seems to be a problem. I am not sure what I am doing wrong. Do you maybe have an example? This relates back to my orginal post. What syntax would you use to send a handle through a mex file?
>
> Regards
>
> Etienne
>
> C============================================
>
> FSTR='@FUN'
> PRHS(1) = mxCreateString(FSTR)
> CALL mxCopyCharacterToPtr(FSTR, PRHS(1), 4)

No. This is not the way to create a function handle in a mex routine. What you have above will probably crash MATLAB since you are copying characters directly into the mxArray structure area of PRHS(1) as a C-style string. And that's not the correct routine to use even if you *were* copying characters into a data area of an mxArray variable. I think Bruno and others assumed you had already passed in the function handle from MATLAB and were simply asking how to use it, hence the suggestion to use feval.

So before we suggest a correction, we need to know the answer to the following question: Do you have a function handle on the MATLAB side that you are passing in to the mex routine as an argument, or do you intend to actually create a function handle mxArray variable from scratch inside the mex routine?

James Tursa
From: Bruno Luong on
Etienne, I do not have fortran compiler handly. I can provide a C-mex code however!

/* function mexcallhandle.c
* Compile:
* >> mex mexcallhandle.c
* Examples:
* >> mexcallhandle(@max, [1 2 3])
* >> mexcallhandle(@sum, [1 2 3])
*/
#include "mex.h"

void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{

mexCallMATLAB(1, plhs, 2, prhs, "feval");

return;
}
From: Etienne on
"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <hlumgp$9oe$1(a)fred.mathworks.com>...
> Etienne, I do not have fortran compiler handly. I can provide a C-mex code however!
>
> /* function mexcallhandle.c
> * Compile:
> * >> mex mexcallhandle.c
> * Examples:
> * >> mexcallhandle(@max, [1 2 3])
> * >> mexcallhandle(@sum, [1 2 3])
> */
> #include "mex.h"
>
> void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
> {
>
> mexCallMATLAB(1, plhs, 2, prhs, "feval");
>
> return;
> }

Hi James

I guess it would be easier to create a function handle in Matlab. I really don't know what the best option would be though. I have not really thought about creating it in the mex file itself. I don't really know how to do that. In the end I would prefer the option that gives the quickest run times.

Regards

Etienne
From: James Tursa on
"Etienne" <etienne.coetzee(a)airbus.com> wrote in message <hluobd$ef$1(a)fred.mathworks.com>...
>
> I guess it would be easier to create a function handle in Matlab. I really don't know what the best option would be though. I have not really thought about creating it in the mex file itself. I don't really know how to do that. In the end I would prefer the option that gives the quickest run times.

The easiest approach would probably be to create the function handles on the MATLAB side and then pass them in and use them. However, I don't really see how this could be significantly different speed-wise than simply calling mexCallMATLAB with the function name directly. If you really wanted a function handle created in the mex routine (not my preferred approach), I don't know of a direct way to do it. I would call mexEvalString with something like "fhandle = @function_name" and then use mexGetVariable to get fhandle. Kind of a clumsy indirect way of doing it but I don't know any other way. (Anybody out there know how to call @ like a function to create a function handle for an arbitrary string input having the name of a function?)

James Tursa
From: Steven Lord on

"James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message
news:hm071a$6bh$1(a)fred.mathworks.com...
> "Etienne" <etienne.coetzee(a)airbus.com> wrote in message
> <hluobd$ef$1(a)fred.mathworks.com>...
>>
>> I guess it would be easier to create a function handle in Matlab. I
>> really don't know what the best option would be though. I have not really
>> thought about creating it in the mex file itself. I don't really know how
>> to do that. In the end I would prefer the option that gives the quickest
>> run times.
>
> The easiest approach would probably be to create the function handles on
> the MATLAB side and then pass them in and use them. However, I don't
> really see how this could be significantly different speed-wise than
> simply calling mexCallMATLAB with the function name directly. If you
> really wanted a function handle created in the mex routine (not my
> preferred approach), I don't know of a direct way to do it. I would call
> mexEvalString with something like "fhandle = @function_name" and then use
> mexGetVariable to get fhandle. Kind of a clumsy indirect way of doing it
> but I don't know any other way. (Anybody out there know how to call @ like
> a function to create a function handle for an arbitrary string input
> having the name of a function?)

To convert a string containing a function name into a function handle, use
STR2FUNC instead of @.

If you're using release R2009a or later, STR2FUNC can also create anonymous
functions.

http://www.mathworks.com/access/helpdesk/help/techdoc/rn/bry1ecg-1.html#bry2cut-1

But be careful, as when they're created from a string like that they're not
created in the scope of the function from which you called STR2FUNC -- I
believe that's why the example in the Release Notes uses NUM2STR.

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ