Problem reading/writing 2D double array

Help
jlid
2013-01-30
2013-05-30
  • jlid

    jlid - 2013-01-30

    Hi,

    I'm trying out a simple case of writing a double array to a .mat file and loading the file in MATLAB R2012a (7.14.0.739) 64-bit Ubuntu 12.01. My version of MatIO is 1.5.0 configured with flags "-prefix=…" & "-with-matlab=…" and successfully passes make check (a bunch of tests were skipped since I didn't enable HDF5 support).

    Starting from a dynamically allocated 1D double matrix of size 16*8 (i.e., a continous 2D array) containing:
    {{1 0 0 0 0 0 0 0}, {0 1 0 0 0 0 0 0}, {0 0 1 0 0 0 0 0}, {0 0 0 1 0 0 0 0},
    {0 1 0 0 0 0 0 0}, {0 0 1 0 0 0 0 0}, {0 0 0 1 0 0 0 0}, {0 0 0 0 1 0 0 0},
    {0 0 1 0 0 0 0 0}, {0 0 0 1 0 0 0 0}, {0 0 0 0 1 0 0 0}, {0 0 0 0 0 1 0 0},
    {0 0 0 1 0 0 0 0}, {0 0 0 0 1 0 0 0}, {0 0 0 0 0 1 0 0}, {0 0 0 0 0 0 1 0}}
    (Note that this is a double array and that I here ignored the decimal part to not clutter the description)

    The way I use MatIO to save the array above (called "M") is as follows (I got this from $MATIO_DIR/test/test.c):

    bool dumpMatFile() {
      mat_t *file = Mat_CreateVer("out.mat", NULL, MAT_FT_DEFAULT);
      if(file == NULL)
         return false;
      size_t dims[2] = {16, 8};
      matvar_t *var = Mat_VarCreate("M", MAT_C_DOUBLE, MAT_T_DOUBLE, 2, dims, this->M, MAT_F_DONT_COPY_DATA);
      if(var == NULL)
         return false;
      Mat_VarWrite(file, var, MAT_COMPRESSION_NONE);
      Mat_VarFree(var);
      Mat_Close(file);
      return true;
    }
    

    But when I load the file "out.mat" in MATLAB I get:

    >> load out
    >> M
    M = 
      1 0 0 0 0 0 0 0
      0 0 1 0 0 0 0 0
      0 1 0 0 1 0 0 0
      0 0 0 1 0 0 1 0
      0 0 0 0 0 1 0 0
      0 0 0 0 0 0 0 1
      0 0 0 0 0 0 0 0
      0 0 0 0 0 0 0 0
      0 0 0 0 0 0 0 0
      1 0 0 0 0 0 0 0
      0 0 1 0 0 0 0 0
      0 1 0 0 1 0 0 0
      0 0 0 1 0 0 1 0
      0 0 0 0 0 1 0 0
      0 0 0 0 0 0 0 1
      0 0 0 0 0 0 0 0
    

    For some reason my matrix ends up being "distorted", why is this?

    I noted that the "rank" argument given to Mat_VarCreate  seems to affect the results as loaded in MATLAB.
    In all the examples I've seen so far rank is statically set to 2, why is this?
    In my case the column/row rank is 7, substituting 2 for 7 however gives an "Error using load\n Out of memory. Type HELP MEMORY for your options" message when I load the resulting file in MATLAB.

    Any suggestions?

     
  • cch

    cch - 2013-01-30

    Your code for the most part looks ok. I think what is messing you up is that MATLAB (and thus matio) uses column major storage format. So for a simple 2x3 matrix
    | 1 3 5|
    | 2 4 6|

    It looks like {1,2,3,4,5,6} in memory. In C row major, it would look like {1,3,5,2,4,6}. So, just swap your dimensions around and {8,16} and you should get the transpose of your matrix in MATLAB. As far as the rank, the rank is the number of dimensions of your data (minimum of 2, and some of the HDF5 code currently assumes no more than 10), and the number of elements is the product of the dimensions in each rank. From your text I think you were under the impression rank is the linear algebra rank of the matrix. Most of the test code uses a rank of 2, but I have definitely written our 3-dimensional data quite often.

    Hope that helps.

    Chris

     
  • jlid

    jlid - 2013-02-04

    Thanks Chris,

    Switching from row-major to column-major ordering did the trick.

    /Jacob

     

Log in to post a comment.