Search code examples
c++matlabmex

Issue with running fmincon with third party mexFunction that returns a struct


I am having a very obvious problem trying to run a third party code as a mex file. The source for this mexfile is available, but I would rather not mess with it. Unfortunately, it returns a struct in outputs, which makes it incompatible with fmincon in MATLAB. Is there anything I can do on the MATLAB side of things so that I don't get : FMINCON requires all values returned by functions to be of data type double.

Or will I have to mess with the actual code?


Solution

  • Can you just wrap the mex function call in a function that breaks out the values as a double and give that to fmincon?

    % MATLAB flavored Pseudocode...
    
    function doubleVal = callMex( input )
        % Function that wraps the mex function call and returns the appropriate type.
        structVal = mex_function( input );
        doubleVal = structVal.someVal;
    end
    
    fmincon(callMex, inputs); % <-- somewhere in another file