how to transfer data from MATLAB to c++ using MEX
Show older comments
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
Accepted Answer
More Answers (0)
Categories
Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!