tbeu / matio

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

empty cell not correctly managed #104

Open Nelson-numerical-software opened 5 years ago

Nelson-numerical-software commented 5 years ago

Tested with 1.5.13:

A = cell(1); 
save('empty_cell.mat', 'A', '-v7.3')

load with matio:

matvar_t * elementMatVar = Mat_VarGetCell(matVariable, (int)k);

elementMatVar->class_type with k = 0 returns: MAT_C_EMPTY and not MAT_C_DOUBLE (expected) elementMatVar->data_type MAT_T_UNKNOWN

same code with a value works:

A = {1} // or  A = {[]}
save('cell.mat', 'A', '-v7.3')
tbeu commented 5 years ago

Is this only the case for v7.3 MAT-files or can you also reproduce when saved with -v6 or -v7?

Nelson-numerical-software commented 5 years ago

same problem with v6 or v7

Nelson-numerical-software commented 5 years ago

save using matio works but not an original .mat file

tbeu commented 5 years ago

Can you please attach all three file here. Thanks.

tbeu commented 5 years ago
>> A = cell(1);
>> B = {[]};
>> isequal(A, B)

ans =

     1
>> whos
  Name      Size                    Bytes  Class

  A         1x1                         4  cell array
  B         1x1                        60  cell array
  ans       1x1                         1  logical array

Though the two variable are equal, they are not. The difference is in the size. Variable A is en empty cell, variable B is a cell containing an empty array (of type double). This is also what matdump reports:

$ matdump -f whos -d empty_cell.mat
Name                       Size           Bytes          Class

A                          1x1                   4  mxCELL_CLASS
B                          1x1                  60  mxCELL_CLASS

$ matdump -d empty_cell.mat A
      Name: A
      Rank: 2
Class Type: Cell Array
{
    Empty
}

$ matdump -d empty_cell.mat B
      Name: B
      Rank: 2
Class Type: Cell Array
{
}

For variable A there simply is no data type information available. That is why you get MAT_T_UNKNOWN. Changing it to MAT_T_DOUBLE by default does not seem straight.

tbeu commented 5 years ago

But I have to admit that this kind of empty variable is not yet covered by the test. In matio_test_cases.m we have the following cases for empty cells

%% Cell-Array Variables
var51 = {}; % or var51 = cell(0);
var52 = {[] single([]) int64([]) uint64([]) int32([]) uint32([]) int16([]) uint16([]) int8([]) uint8([])};