MATLAB: Is it possible to pass functions as arguments to C++ MEX functions

cmex

I have only been working with mex functions for a couple of weeks, and am now working on writing a Runge-Kutta, 4th order solver as a C++ mex function.
I am wondering whether it is possible to take a function as an input.
Effectively, it would be nice to have my dynamics function written in MATLAB and pass it straight through to my RK4 mex function.
For example, if the dynamics are governed by Duffing's equation:
function xdot = Duffing(t,x)
xdot = [x(2); 0.3*cos(t)-0.22*x(2)+x(1)-x(1)^3];
end
(I do realize this can be written in line as Duffing = @(t,x) (whatever) also.
Is there a way to call the dynamics function from within a mex function or is inputs[] constrained to numeric types only?
I tried the following:
class MexFunction : public matlab::mex::Function {
public:
void operator()(ArgumentList outputs, ArgumentList inputs) {
// Access the dynamics function
typedef TypedArray<double> xdot_type (TypedArray<double>, TypedArray<double>);
xdot_type xdot;
xdot = inputs[0];
Naturally, this doesn't work because inputs[0] is not assignable to 'TypedArray<double> (TypedArray<double>, TypedArray<double>)', since I think the ArgumentList thinks inputs[0] should be purely numeric.
Can anyone think of a solution to this, or will I just have to write my dynamics function in C++?
Looking forward to some suggestions!
Thomas

Best Answer

Just quickly skimming the MATLAB C++ API doc, it looks like you can do this using the matlab::engine::MATLABEngine::feval interface found here:
I.e., pass your function name into the C++ mex file as a string which you can get from the ArgumentList via the matlab::data::CharArray syntax:
void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) {
matlab::data::CharArray fname = inputs[0];
etc.
Then convert fname into a std::u16string and use that in the feval interface.