how to transfer data from MATLAB to c++ using MEX
이전 댓글 표시
I use following code to transfer data from c++ to matlab:
void function(vector<vector<double>> mat)
{
size_t rows = mat.size();
size_t cols = mat[0].size();
mxArray* T = mxCreateDoubleMatrix(rows, cols, mxREAL);
double * in_buf = (double *)mxGetPr(T);
for (int i = 0; i<rows; i++)
{
std::memcpy(in_buf + i * cols, &mat[i][0], cols *sizeof(double));
}
}
and pass the mat into matlab function say pca(T)
then
engEvalString(ep, "[coeff , latent] = pca(T);");
my question is how to get the data calculated above and store them in different vectors:
mxArray* coef = engGetVariable(ep, " coeff' ");
mxArray* latent = engGetVariable(ep, " latent' ");
vector<double> latents(rows);
vector<vector<double>> coeff(rows, vector<double>(cols));
memcpy((void *)latents,(void *)mxGetPr(latent), rows *sizeof(double));
memcpy((void *)latents,(void *)mxGetPr(coef), rows *sizeof(double));
I used std::memcpy to do this but it doesn't work ?
Any help will be greatly appreciated.
Regards
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Call C/C++ from MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!