Azzera filtri
Azzera filtri

how to use mexCallMATLAB with fmincon

2 visualizzazioni (ultimi 30 giorni)
Kaushik
Kaushik il 15 Ott 2012
hi,
it would be kind of someone to clarify a couple of issues i am having.
i am new to matlab so some please give me a detailed answer if possible.
i am trying to use mexCallMATLAB() to call fmincon inside a c++ code.
this is what i have inside my c++ function
functionCPP()
{
// first i define the input and output arrays
mxArray *output[2], *input[8] ;
//then i call mexcall matlab
mexCallMATLAB(2, output, 8, input, "fmincon");
}
now the questions are:
1> should the "input" array zero based or 1 bases. meaning input should have the arguments of fmincon function. so should input[0] be the objective function handle to fmincon or should it be input[1] ?
2> assuming zero based indexing the input[2] is a matrix which is the 3rd argument of fmincon. should it be column major or row major ?
3> the most important issue is the my objective function is also written in c++. how should i pass the handle of the objective function to input[0] (which is the first argument of fmincon). This the the part i need some detailed clarification as to how can i make the whoel thing work.
thanks in advance.

Risposte (1)

James Tursa
James Tursa il 15 Ott 2012
Modificato: James Tursa il 15 Ott 2012
1) input[0] should contain the function handle. If you need to create a function handle from within the mex function, you can use mexCallMATLAB to call the MATLAB function str2func.
2) input[2] should have the same internal memory layout for the data as a regular MATLAB variable, hence it should be column major (like Fortran), not row major (like C/C++).
3) You need to create a function at the MATLAB level that can be called by fmincon. Let's assume for the moment that your objective function C++ code is within the same mex function that we are discussing (i.e., the one that is calling mexCallMATLAB to subsequently call "fmincon". Then you could create a separate m-file with the appropriate argument signature for fmincon. This m-file could in turn call your original mex function (i.e., a recursive call) but with a special additional input flag that tells the mex function it is being called from fmincon. That way the mex function can call the C++ objective function and return the result to fmincon. If the objective function depends on other inputs besides the arguments (i.e., some global variables or settings), you can have those at the top level of your C++ code so that they remain static and accessible from all the recursive calls. If you can simply separate your objective function into a separate mex function then you can go that route also (but it won't be able to easily share non-argument data with the original calling mex function.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by