Accessing elements in an mxArray

조회 수: 7 (최근 30일)
Caroline
Caroline 2013년 7월 24일
I am using the MATLAB engine to read a excel file using C++. I am trying to access elements in an mxArray that was created using engEvalString and xlsread, they were used in the following way:
mxArray *num;
mxArray *text;
mxArray *raw;
engEvalString(ep, "[num, text, raw] = xlsread('C:\\rest_of_file_path')")
However, now I am trying to access the elements of raw by using mxGetCell(*mxArray, int ind). I know that this returns a pointer to the cell with the index ind.I used mxGetCell in the following way (dereference the pointer to return the actual value in the cell with the specified index) :
cout << *mxGetCell(num, 40) << endl;
but I got the following error:
binary '<<': no operator found which takes right-hand operand of type 'mxArray' (or there is no acceptable conversion).
The right hand operator shouldn't be an mxArray, shouldn't it be a number? What is wrong with they way I am dereferencing the pointer? How could I get the values in the mxArray?
Any help that anyone could provide would be much appreciated.
Thanks!

채택된 답변

James Tursa
James Tursa 2013년 7월 24일
mxGetCell(num, 40) returns an (mxArray *), i.e. a pointer to an mxArray. Thus, by extension, *mxGetCell(num, 40) deferences that pointer into an mxArray. Hence the error message you are seeing. Each "element" of a cell array is an (mxArray *). You need to get at the data of the underlying mxArray. E.g.,
cout << *mxGetPr(mxGetCell(num, 40)) << endl;
  댓글 수: 3
James Tursa
James Tursa 2013년 7월 25일
편집: James Tursa 2013년 7월 25일
Since you have both that you need to deal with and you won't know until runtime what you have, you will need to wrap some logic around your output statement. E.g., something like this
char *cp;
mxArray *cell;
:
cell = mxGetCell(num,1);
if( cell == NULL ) {
; // Do nothing this branch since there is nothing in the cell
} else if( mxIsChar(cell) ) {
cp = mxArrayToString(cell); // MATLAB char array to C-style string
cout << cp << endl;
mxFree(cp); // Free the dynamically allocated C-style string
} else {
cout << *mxGetPr(cell) << endl; // Assume mxArray is double
}
Caroline
Caroline 2013년 7월 26일
Thank you so much for your help!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Transaction Cost Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by