using matlab engine from c++: How to convert a int** into mxArray*
古いコメントを表示
Hi,
I am opening matlab engine from c++, and I want to find the inverse matrix of a matrix. I have tried to convert the int** matrix into mxArray* in different ways, without success:
S = mxCreateDoubleMatrix(v->getCols(), v->getRows(), mxREAL);
memcpy(mxGetPr(S), v->getOpSet(), sizeof(v->getOpSet()));
or:
S = mxCreateDoubleMatrix(v->getCols(), v->getRows(), mxREAL);
mxSetPr(S,(double*)v->getOpSet());
double *acc =mxGetPr(S);
for(p=0;p<mxGetM(S); p++){
for(p1=0 ; p1<mxGetN(S) ; p1++){
cout<< acc[mxGetM(S)*p1+p];
}
}
cout<<endl;
On my first try, the first 2 elements are exponential and weird, and the rest of the matrix is 0.
on my second try, all of the elements are weird, and I get segmentation fault.
have someone have some experience with this and knows how to make the convertion?
another problem I have is that I can't get the result in an array, only as string. meaning,how can I get 'x' as a double array (not mxArray) after this:
buffer[BUFSIZE] = '\0';
engOutputBuffer(ep, buffer, BUFSIZE);
engEvalString(ep, "x = mldivide(S,T)");
(after this code, buffer contains the right result, but if I do:
D=engGetVariable(ep, "x");
D doesn't contain the right result.
is there a way to do this right?
thank you.
採用された回答
その他の回答 (2 件)
Pierre
2011 年 8 月 29 日
I didn't really get your posted code as it requires some guessing, but let me point out a few things:
1. While you might be used to row-major order from C++ code/libraries, MATLAB uses column-major order. This requires you to manually reorganize data when passing from one representation to the other one. (And, most likely it should be mxCreateDoubleMatrix(v->get Rows(), v->get Cols(), mxREAL);
2. To be honest, I didn't check that explicitely, but after years of excessive C++ coding, I'd personally never expect the canonical casting of an array's content to work: Casting an int to double, is something different than casting an int* to a double* !!!
int i = 1234;
double d = (double)i; // d=1234.0
but
int* i_ptr = new int;
*i_ptr = 1234;
double* d_ptr = (double*) i_ptr; // (*d_ptr) = ???
(*d_ptr) will read out the integer represenation of 1234 as if it was stored as a double in memory.
Meytal
2011 年 8 月 29 日
0 投票
4 件のコメント
James Tursa
2011 年 8 月 29 日
It should be easy to get the C++ array into MATLAB. Can you please provide more detail about your variables? What, exactly, is v (type and size)? What, exactly, does getOpSet() return (type)? As has already been posted, you will have to manually copy the data into the mxArray data area (do NOT use mxSetPr), and the column-major-order vs row-major-order thing DOES make a difference, even if your matrix is square.
Meytal
2011 年 8 月 29 日
James Tursa
2011 年 8 月 29 日
What does the (double **) value that is returned by getOpSet point to? E.g., if I do this: double **x = v->getOpSet(), then what does x point to? What does *x point to? Is your array data contiguous in memory, and if so how would I get the starting address of the data from x?
Meytal
2011 年 8 月 29 日
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!