Problem extracting data out of a variable of mat file in C++
이전 댓글 표시
I have to extract the data out of a variable(double array) of a mat file for use in my C++ code.I have tried the mxGetPr, mxGetVariable and mxGetData functions but they do not seem to work.I am able to read the mat file and even get the variable's name and no. of rows and columns but I haven't been able to extract data.Please help me out ,need urgent help!!
댓글 수: 2
James Tursa
2012년 6월 26일
Please post you code so we can point out the errors and suggest corrections.
Qandeel
2012년 6월 27일
편집: Walter Roberson
2012년 6월 28일
답변 (1개)
James Tursa
2012년 6월 28일
Your incorrect code:
/*Get Variable*/
pa = matGetVariable(pmat, file);
The 2nd argument is supposed to be the variable name, not the file name. E.g.,
/*Get Variable*/
pa = matGetVariable(pmat, dir[i]);
Then you can get at the data using mxGetPr(pa) etc. Just be sure not to free dir before you are done with it. And when you are done with it, free each dir[i] first since all of these strings are dynamically allocated also.
댓글 수: 9
Qandeel
2012년 6월 28일
편집: Walter Roberson
2012년 6월 28일
James Tursa
2012년 6월 28일
"... Just be sure not to free dir before you are done with it. ..."
You did not follow these instructions. You have mxFree(dir) inside your loop, so after the first iteration dir is no longer valid. Then on the 2nd iteration through the loop you access dir[i], which is invalid because you just free'd dir on the previous iteration.
Solution: You can put mxFree(dir[i]) inside your loop at the end (but not inside the mxIsNumeric(pa) check) to free the individual name strings, but you cannot have mxFree(dir) inside the loop. You need to move that outside the loop.
Qandeel
2012년 6월 28일
James Tursa
2012년 6월 28일
That's because you declared it to be const for some reason. Change this:
const char **dir;
to this:
char **dir;
Qandeel
2012년 6월 28일
James Tursa
2012년 6월 29일
I took a closer look at the matGetDir function and looked at all the addresses returned for the pointers and strings used and have come to the conclusion that I was wrong about the individual string allocations. It turns out that the pointer array and the strings themselves are all part of one big contiguous memory block. Everything is sitting behind the dir address. So get rid of the mxFree(dir[i]) stuff completely. Just have one mxFree(dir) call at the end of your code (outside the loop).
Qandeel
2012년 6월 30일
Ketan
2012년 7월 1일
The %d format specifier indicates an integer argument, but I do not think the compiler converts the double *pr to int.
Try casting *pr from a double to an int datatype. For example:
...%d\n",(int)(*pr));
You only see one element because you are not iterating over the #elements in the array (the variable l).
This stack overflow article goes into some detail regarding the printf issue:
Qandeel
2012년 7월 1일
카테고리
도움말 센터 및 File Exchange에서 Whos에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!