dynamically allocated c++ array covert-convert to mxArray

Hello every one, i am new to matlab. So what i am trying to do is solve a problem like: Ax=B
I get a nXn matrix from my c++ code and i want to pass its values to mxArray. Specifically i do something like:
double **Ac = NULL;
Ac = new double*[size];
for(i = 0; i < size; i++){
Ac[i] = new double[size];
}
mxArray *Am = NULL;
Am = mxCreateDoubleMatrix (size, size, mxREAL);
memcpy( ( void* )mxGetPr(Am), ( void* )Ac, size*size*sizeof(double));
engPutVariable(ep, "Am",Am);
engEvalString(ep, "plot(Am);");
It shows a plot but not the correct one. Any directions?
Thank you all

 採用された回答

Jan
Jan 2013 年 7 月 31 日

0 投票

Ac is not an array which contains the double values, but an array of pointers to vectors, which contain the values. If you prefer this nested kind of data storage for any good reasons, you need to copy the data vector by vector also:
double *Am_p;
Am_p = mxGetPr(Am);
for (i = 0; i < size; i++) {
memcpy(Amp, Ac[i], size * sizeof(double));
Amp += size;
}

2 件のコメント

Pavlos
Pavlos 2013 年 7 月 31 日
Yes this seems right but i did it in a different way:
Ac = new double*[size];
Ac[0] = new double[size*size];
for (i=1; i < size ; i++){
Ac[i] = Ac[i-1] + size;
}
mxArray *Am = NULL;
Am = mxCreateDoubleMatrix (size, size, mxREAL);
memcpy( ( void* )mxGetPr(Am), ( void* )Ac[0], size*size*sizeof(double));
and it worked fine. Thanks a lot!
Jan
Jan 2013 年 7 月 31 日
I prefer storing matrices in one contiguous block also.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeC Shared Library Integration についてさらに検索

質問済み:

2013 年 7 月 31 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by