M.Lanus
M.Lanus

Reputation: 11

Opening a 3d matlab file in C++ with Matio

So I have a matlab file that is a 3d matrix. I am using C++ to read in the file, specifically with matio.h but I am having some trouble/confusion with reading the actual data. I know how to find out how many dimensions, rank, and name of the data, but to actually read in the data is the hard part. Currently I have

 mat_t *mat = Mat_Open(result, MAT_ACC_RDONLY);
 matvar_t *matvar;
 matvar = Mat_VarReadNExtInfo(mat);
 int r = matvar->dims[0];

I guess I am confused as to how to use the matvar->data pointer.

Upvotes: 1

Views: 664

Answers (1)

Nils
Nils

Reputation: 2695

You can select your variable by using:

matvar_t *matVar = NULL;    
matVar = Mat_VarRead(mat, (char*)"VarName");

Reading the data is possible through:

unsigned Size = matVar->nbytes/matVar->data_size ;
const double *Data = static_cast<const double*>(matVar->data) ;
for(int i=0; i<Size; ++i)
{
     std::cout<<"\t["<<i<<"] = "<<Data[i]<<"\n" ;
}

As it is C Code the values are behind each other in memory. With this solution you have to reshape the double array into an 3D Array on your own by using the dimensions.

Another solution could be using the function Mat_VarReadDataAll.

Upvotes: 1

Related Questions