I am making a program in which, I am doing some processing in matlab then save the result in .mat file using following command in matlab,
save data x;
let suppose value of x=2,
Now I am reading the same data.mat file from C++ program in Visual studio 2010. My program is compiling and I can also read the name of arrays and its dimensions perfectly,.. Now the problem is when I am using following command, I cannot read the exact value of x. It is showing me some random values each time I run the program.
variable = matGetNextVariable(pmat, &name);
right now value of variable =50779048..
Kindly guide me where I am making mistakes. the value of variable should be 2, because I have saved 2 from matlab cammand..
I already check this question but it seems nobody answered it, Reading .mat file in C++
void main(int argc, char **argv)
{
MATFile *pmat;
const char* name=NULL;
mxArray *pa;
/* open mat file and read it's content */
pmat = matOpen("data.mat", "r");
if (pmat == NULL)
{
printf("Error Opening File: \"%s\"\n", argv[1]);
return;
}
/* Read in each array. */
pa = matGetNextVariable(pmat, &name);
while (pa!=NULL)
{
/*
* Diagnose array pa
*/
printf("\nArray %s has %d dimensions.", name,
mxGetNumberOfDimensions(pa));
//print matrix elements
printf("\ndata %d",pa);
//get next variable
pa = matGetNextVariable(pmat,&name);
//printf("\ndata %d",pa);
//destroy allocated matrix
mxDestroyArray(pa);
}
matClose(pmat);
}
Thank you.
After doing a lot of search again, I found the answer. Somebody already asked the questioned but in different way, Following is the link of answer. I hope it might help others. Matlab API reading .mat file from c++, using STL container