tbeu / matio

MATLAB MAT File I/O Library
https://matio.sourceforge.io
BSD 2-Clause "Simplified" License
334 stars 97 forks source link

Reading 2d complex double array does not work #87

Closed Masterbaggins closed 6 years ago

Masterbaggins commented 6 years ago

Hello,

is it possible to read a complex valued Matrix from some .mat file using your software? I couldn't figure out how this is done.

Considering this .mat file, I want to read the variable usol, which is a 2d complex double array of size 256x101. The code looks like this:

#include <iostream>
#include "matio.h"

int main(void)
{
  mat_t* matfp;
  matvar_t* usol_var;

  matfp = Mat_Open("burgers.mat", MAT_ACC_RDONLY);

  usol_var = Mat_VarRead(matfp, "usol");

  mat_complex_split_t* usol_data = (mat_complex_split_t*) usol_var->data;

  // Gives correct results:
  std::cout << *((double*)usol_data[0].Re) << "\n";
  // Won't work:
  // std::cout << *((double*)usol_data[1].Re) << "\n";

  Mat_Close(matfp);

  return 0;
}

I get correct results for the really first entry of the matrix. But moving forward the usol_var pointer, I get inconsistent results. On the other hand, reading standard double vectors (t,x) from data works fine.

tbeu commented 6 years ago
#include <iostream>
#include "matio.h"

int main(void)
{
    mat_t* matfp = Mat_Open("burgers.mat", MAT_ACC_RDONLY);
    matvar_t* usol_var = Mat_VarRead(matfp, "usol");
    mat_complex_split_t* usol_data = (mat_complex_split_t*)usol_var->data;

    double* Re = (double*)(usol_data->Re);
    double* Im = (double*)(usol_data->Im);
    std::cout << Re[0] << " + " << Im[0] << "i\n";
    std::cout << Re[1] << " + " << Im[1] << "i\n";

    Mat_VarFree(usol_var);
    Mat_Close(matfp);

    return 0;
}
Masterbaggins commented 6 years ago

Oh, I see :). Thanks a lot!