Problem extracting data out of a variable of mat file in C++
Show older comments
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 Comments
James Tursa
on 26 Jun 2012
Please post you code so we can point out the errors and suggest corrections.
Qandeel
on 27 Jun 2012
Edited: Walter Roberson
on 28 Jun 2012
Answers (1)
James Tursa
on 28 Jun 2012
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 Comments
Qandeel
on 28 Jun 2012
Edited: Walter Roberson
on 28 Jun 2012
James Tursa
on 28 Jun 2012
"... 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
on 28 Jun 2012
James Tursa
on 28 Jun 2012
That's because you declared it to be const for some reason. Change this:
const char **dir;
to this:
char **dir;
Qandeel
on 28 Jun 2012
James Tursa
on 29 Jun 2012
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
on 30 Jun 2012
Ketan
on 1 Jul 2012
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
on 1 Jul 2012
Categories
Find more on Workspace Variables and MAT 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!