在C ++代码中调用Matlab - 使用engine.h中的方法 [英] Call Matlab in C++ code - using methods in engine.h
问题描述
我写一个C ++程序,其唯一的目的是调用Matlab代码。我有一个主程序,将
-
将文件(高维度为90000 * 24的矩阵)中的数据读入C ++
结构 -
将这些结构传递到Matlab代码
-
这些结构在参数
-
中获取来自Matlab的输出数据并将其存储在C ++结构中
在2 /中,矩阵是Matlab结构中的字段。该结构,例如 MATLAB_STRUCT
有几个矩阵字段, MATLAB_STRUCT.Z1
, MATLAB_STRUCT.Z2
,...和一些浮动字段 MATLAB_STRUCT.flt1
,...
是将C ++矩阵( double **
)设置为Matlab结构的字段的正确方法?到目前为止,我想出了这个,使用 engine.h
mxArray * labcoeffs_array = convertVectorToMxArray(labcoeffs,
coeff_nrows,coeff_ncols);
const std :: string lab_coeff_name =MATLAB_STRUCT.labCoef;
engPutVariable(ep,lab_coeff_name.c_str(),labcoeffs_array);
其中 convertVectorToMxArray
将 double **
转换为 mxArray
,
inline mxArray * convertVectorToMxArray(double ** mat,
const int nb_rows,const int nb_cols)
{
mxArray * outputMxArray = mxCreateDoubleMatrix(
int)nb_rows,
(int)nb_cols,
mxREAL);
double * data =(double *)mxGetData(outputMxArray);
for(int r = 0; r for(int c = 0; c data [r + c * nb_rows] = double)mat [r] [c];
return outputMxArray;
};
但我已经看到了一些其他的技术用于在Cpp代码中为一个Matlab结构赋值但不是矩阵),在C ++字符串中模仿命令行语法:
std :: string setStruct = MATLAB_STRUCT+。 +nB+=+ str-> nB +;;
matlabExecute(ep,setStruct);
与 ep
指向Matlab引擎。
-
是否可以使用命令行将此方法适用于为Matlab struct的矩阵类型字段赋值?
-
为Matlab结构的矩阵类型字段分配值的最佳方法是什么?
要在结构中创建一个矩阵字段,一个选项是创建一个temp变量,然后将其值赋给一个字段的结构:
//创建临时变量
mxArray * array = convertVectorToMxArray(mat,nb_rows,nb_cols);
const std :: string temp_name = array_name +_temp;
int ret = engPutVariable(ep,temp_name.c_str(),array);
//将变量设置为struct字段
const std :: string cmd = std :: string(array_name +=+ temp_name +;);
matlabExecute(ep,cmd);
//删除数组
mxDestroyArray(array);
I write a C++ program whose only purpose is to call Matlab code. I have a main routine, which
read data in a file (matrices with high dimension 90000*24) into C++ structures
pass these structures to Matlab code
launch the Matlab routine with these structures in argument
get the output data from Matlab and store them in C++ structures
In 2/, matrices are fields in a Matlab struct. The struct, say MATLAB_STRUCT
has several matrix fields, MATLAB_STRUCT.Z1
, MATLAB_STRUCT.Z2
,... and some float fields MATLAB_STRUCT.flt1
,...
What is the correct approach to set C++ matrices (double**
) as the fields of the Matlab struct? So far, I came up with this, using engine.h
mxArray* labcoeffs_array = convertVectorToMxArray(labcoeffs,
coeff_nrows, coeff_ncols);
const std::string lab_coeff_name = "MATLAB_STRUCT.labCoef";
engPutVariable(ep, lab_coeff_name.c_str(), labcoeffs_array);
where convertVectorToMxArray
is an helper I wrote to convert double**
to a mxArray
,
inline mxArray *convertVectorToMxArray(double** mat,
const int nb_rows, const int nb_cols)
{
mxArray *outputMxArray = mxCreateDoubleMatrix(
(int) nb_rows,
(int) nb_cols,
mxREAL);
double *data = (double*) mxGetData(outputMxArray);
for (int r = 0; r < nb_rows; r++)
for (int c = 0; c < nb_cols; c++)
data[r + c*nb_rows] = (double)mat[r][c];
return outputMxArray;
};
But I have seen some other technique for assigning a value to a Matlab struct in the Cpp code (a float value though, not a matrix), imitating the command line syntax in a C++ string:
std::string setStruct = "MATLAB_STRUCT" + "." + "nB" + " = " + str->nB + ";";
matlabExecute(ep, setStruct);
with ep
a pointer to a Matlab engine.
Is it possible to adapt this approach with command line to assigning a value to a matrix type field of a Matlab struct?
what is the best approach to assign a value to a matrix type field of a Matlab struct?
To create a matrix field in a struct, one option is to create a temp variable, and then to assign its value to a field of the struct:
// Create temp variable
mxArray* array = convertVectorToMxArray(mat, nb_rows, nb_cols);
const std::string temp_name = array_name + "_temp";
int ret = engPutVariable(ep, temp_name.c_str(), array);
// Set variable to struct field
const std::string cmd = std::string(array_name + " = " + temp_name + "; ");
matlabExecute(ep, cmd);
// Delete array
mxDestroyArray(array);
这篇关于在C ++代码中调用Matlab - 使用engine.h中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!