
#in c lu d e " wrapper . h"
#in c lu d e " libcppdemo . h"
// co n structor
Temp late _ Comp onen t :: T empl ate_ Comp o nen t ()
{
// i n itialize MATLAB envi r onment
libc ppde moIn itia liz e ();
}
// d estructor
Temp late _ Comp onen t ::~ Tem plat e_Co m pone nt ()
{
// termin a t e the MatLab e n vironme n t
libc ppde moTe rmin ate ();
}
// example for simple member function
void Temp l ate_ Comp onen t :: s u bscri be_de m o ( double * Matrix , ... , double * Result )
{
// define variables
i n t i , j;
double * Matrix_M = new double [ Dim1 * Dim2 ];
double * Result_M = new double [ Dim1 * Dim2 ];
mxA rray * pmxInput , * pmxNumber , * pmxResult , * pmxRe s ult2 ;
// convert the input - matrix from C - style to MATLAB - style
f o r (i =0; < Dim1 ; i ++) {
f o r (j =0; j< Dim2 ; j++) {
Matrix_M [j * Dim1 +i ] = Matrix [i * Dim2 +j ];
}
}
// call compiled MATALB function
mlfAssign (& pmxInput , ml fDoub leMat rix ( Dim1 , Dim2 , Matrix_M , NULL ));
mlfAssign (& pmxResult , ml fDou b leMa t rix ( Dim1 , Dim2 , Result , NULL ));
mlfAssign (& pmxResult , mlfDemo ( pmxInput , pmxNumber ));
// read data from MATLAB interface arrays back to C variables
double * pResult = m xGetPr ( pmxRe s u lt );
double * pError = mxGetPr ( pmxE r rorcod e );
f o r (i =0; < Dim1 ; i ++) {
f o r (j =0; j< Dim2 ; j++) {
Result_M [i * Dim2 +j ] = p R esult [ j* Dim1 + i ];
}
}
memcpy ( Result , Result_M , Dim1 * Dim2 * s i z e o f ( double ));
// free memory : MATLAB - arrays , t emporary MATLAB - style matrices
d e l e t e [] Matrix_M ;
mxDes t royAr ray ( pmxInput );
...
}
Listing 6: wrapper.cpp
18
Commenti su questo manuale