@@ -18,8 +18,9 @@ class MatlabCaller
1818
1919 void setEngine (){
2020 if (!(matlabPtr = engOpen (" " ))) {
21- throw (" \n Can't start MATLAB engine\n " );
21+ throw std::runtime_error (" \n Can't start MATLAB engine\n " );
2222 }
23+ engSetVisible (matlabPtr, 0 );
2324 };
2425
2526 void startMatlab (){
@@ -31,16 +32,52 @@ class MatlabCaller
3132 dirChanged = true ;
3233 };
3334
35+ void call (std::string functionName, std::vector<double >& xdata, std::vector<double >& params, std::vector<double >& output)
36+ {
37+ if (!this ->matlabPtr )
38+ this ->setEngine ();
39+
40+ if (dirChanged){
41+ std::string cdCmd = " cd('" + (this ->currentDirectory + " ')" );
42+ engEvalString (this ->matlabPtr , cdCmd.c_str ());
43+ }
44+
45+ dirChanged = false ;
46+ mxArray *XDATA = mxCreateDoubleMatrix (1 ,xdata.size (),mxREAL);
47+ memcpy (mxGetPr (XDATA), &xdata[0 ], xdata.size ()*sizeof (double ));
48+ engPutVariable (this ->matlabPtr , " xdata" , XDATA);
49+ mxArray *PARAMS = mxCreateDoubleMatrix (1 ,params.size (),mxREAL);
50+ memcpy (mxGetPr (PARAMS), ¶ms[0 ], params.size ()*sizeof (double ));
51+ engPutVariable (this ->matlabPtr , " params" , PARAMS);
52+
53+ std::string customCmd = " [output, subRough] = " + (functionName + " (params, bulkIn, bulkOut, contrast)" );
54+ engPutVariable (this ->matlabPtr , " myFunction" , mxCreateString (customCmd.c_str ()));
55+ engOutputBuffer (this ->matlabPtr , NULL , 0 );
56+ engEvalString (this ->matlabPtr , " eval(myFunction)" );
57+
58+ mxArray *matOutput = engGetVariable (this ->matlabPtr , " output" );
59+ if (matOutput == NULL )
60+ {
61+ throw std::runtime_error (" ERROR: Results could not be extracted from MATLAB engine." );
62+ }
63+
64+ const mwSize* dims = mxGetDimensions (matOutput);
65+ double * s = (double *)mxGetData (matOutput);
66+ for (int i=0 ; i < dims[0 ] * dims[1 ]; i++)
67+ output.push_back (s[i]);
68+ };
69+
3470 void call (std::string functionName, std::vector<double >& params, std::vector<double >& bulkIn,
3571 std::vector<double >& bulkOut, int contrast, int domain, std::vector<double >& output, double * outputSize, double * rough)
3672 {
3773 if (!this ->matlabPtr )
3874 this ->setEngine ();
75+
3976 if (dirChanged){
4077 std::string cdCmd = " cd('" + (this ->currentDirectory + " ')" );
4178 engEvalString (this ->matlabPtr , cdCmd.c_str ());
4279 }
43- // this->matlabPtr->feval(u"cd", factory.createCharArray(this->currentDirectory));
80+
4481 dirChanged = false ;
4582 mxArray *PARAMS = mxCreateDoubleMatrix (1 ,params.size (),mxREAL);
4683 memcpy (mxGetPr (PARAMS), ¶ms[0 ], params.size ()*sizeof (double ));
@@ -52,39 +89,34 @@ class MatlabCaller
5289 memcpy ((void *)mxGetPr (BULKOUT), &bulkOut[0 ], bulkOut.size ()*sizeof (double ));
5390 engPutVariable (this ->matlabPtr , " bulkOut" , BULKOUT);
5491 mxArray *CONTRAST = mxCreateDoubleScalar (contrast);
55- // memcpy((void *)mxGetPr(CONTRAST), &contrast, 1*sizeof(double));
5692 engPutVariable (this ->matlabPtr , " contrast" , CONTRAST);
57- // if (domain > 0)
58- // args.push_back(factory.createScalar<int>(domain));
59- std::string customCmd = " [output, subRough] = " + (functionName + " (params, bulkIn, bulkOut, contrast)" );
93+ std::string customCmd;
94+ if (domain > 0 ){
95+ mxArray *DOMAIN_NUM = mxCreateDoubleScalar (domain);
96+ engPutVariable (this ->matlabPtr , " domain" , DOMAIN_NUM);
97+ customCmd = " [output, subRough] = " + (functionName + " (params, bulkIn, bulkOut, contrast, domain)" );
98+ }
99+ else {
100+ customCmd = " [output, subRough] = " + (functionName + " (params, bulkIn, bulkOut, contrast)" );
101+ }
60102 engPutVariable (this ->matlabPtr , " myFunction" , mxCreateString (customCmd.c_str ()));
61103 engOutputBuffer (this ->matlabPtr , NULL , 0 );
62- // auto start = high_resolution_clock::now();
63- // std::vector<matlab::data::Array> results = this->matlabPtr->feval(functionName, 2, args);
64104 engEvalString (this ->matlabPtr , " eval(myFunction)" );
65- // auto stop = high_resolution_clock::now();
66- // auto duration = duration_cast<microseconds>(stop - start);
67- // std::cout << duration.count() << "Usec" << std::endl;
68105
69106 mxArray *matOutput = engGetVariable (this ->matlabPtr , " output" );
70- if (matOutput == NULL )
71- {
72- throw (" FAILED!" );
73- }
74- mxArray *subRough = engGetVariable (this ->matlabPtr , " subRough" );
75- if (subRough == NULL )
107+ mxArray *subRough = engGetVariable (this ->matlabPtr , " subRough" );
108+ if (matOutput == NULL || subRough == NULL )
76109 {
77- throw ( " FAILED! " );
110+ throw std::runtime_error ( " ERROR: Results could not be extracted from MATLAB engine. " );
78111 }
112+
79113 *rough = (double )mxGetScalar (subRough);
80114 const mwSize* dims = mxGetDimensions (matOutput);
81115 outputSize[0 ] = (double ) dims[0 ];
82116 outputSize[1 ] = (double ) dims[1 ];
83- // output.push_back((double) matOutput[i]);
84117 double * s = (double *)mxGetData (matOutput);
85118 for (int i=0 ; i < dims[0 ] * dims[1 ]; i++)
86119 output.push_back (s[i]);
87- // std::memcpy(output, (double *)mxGetData(matOutput), mxGetNumberOfElements(matOutput)* mxGetElementSize(matOutput));
88120 };
89121
90122 static MatlabCaller* get_instance ()
0 commit comments