MAT.jl icon indicating copy to clipboard operation
MAT.jl copied to clipboard

cannot read tables that are nested into a struct

Open lmulder1991 opened this issue 2 years ago • 3 comments

I tried to load an object that I converted to a struct. The struct is correctly parsed into a Dict, but the table itself gets converted into another Dict with the "S0/1/2" & "arr" keys instead of the table it self. In the exampe I created a simple table, but it happens to any table I think, at least also my more complex table ends up in this same format.

julia> using MAT

julia> file = matread(raw"path_to\test_data.mat")
Dict{String, Any} with 2 entries:
  ""  => UInt8[0x00 0x01 … 0x00 0x00]
  "s" => Dict{String, Any}("data"=>Dict{String, Any}("s1"=>Int8[77, 67, 79, 83], "arr"=>("", UInt32[0xdd000000; 0x00000002; … ; 0x00000001; 0x00000002;;]), "s0"=>Int8[], "s2"=>Int8[116, 97, 98, 108, 101]))

julia> file["s"]["data"]
Dict{String, Any} with 4 entries:
  "s1"  => Int8[77, 67, 79, 83]
  "arr" => ("", UInt32[0xdd000000; 0x00000002; … ; 0x00000001; 0x00000002;;])
  "s0"  => Int8[]
  "s2"  => Int8[116, 97, 98, 108, 101]

test_data.zip

lmulder1991 avatar Oct 24 '23 07:10 lmulder1991

To clarify, what kind of "table" would you like to see? A DataFrame from DataFrames.jl?

mkitti avatar Oct 25 '23 14:10 mkitti

Yes; that would work for me

lmulder1991 avatar Oct 25 '23 19:10 lmulder1991

Facing the same/similar issue here with tables nested within tables. As an example, consider the attached file subtable.zip (containing a subtable.mat). subtable.zip It was created, I think, with the following MNWE script:

x = [11; 12];
y = [13; 14];
t1 = table(x, y);
x = [21; 22];
y = [23; 24];
t2 = table(x, y);
s.subtable = t1;
s(2).subtable = t2;

It contains a table, of which one of the fields is itself a table. Upon reading it in Julia, the result is not what I would hope:

>> loaded = load('subtable.mat');
>> loaded.s(1).subtable
 
ans =
 
  2×2 table
 
    x     y 
    __    __
 
    11    13
    12    14
 
but in Julia the result is weird...
 
julia> loaded = MAT.matread("subtable.mat")
Dict{String, Any} with 2 entries:
  ""  => UInt8[0x00 0x01 … 0x00 0x00]
  "s" => Dict{String, Any}("subtable"=>Any[Dict{String, Any}("s1"=>Int8[77, 67, 79, 83], "arr"=>(""[…]

KeithWM avatar Mar 04 '24 19:03 KeithWM

This should work now in MAT v0.11. Tried with the struct array of tables example here. I also tried with tables inside tables as well.

In MATLAB:

x = [11; 12];
y = [13; 14];
t1 = table(x, y);
x = [21; 22];
y = [23; 24];
t2 = table(x, y);
s.subtable = t1;
s(2).subtable = t2;
save('test.mat', 's')

In Julia:

julia> using MAT, DataFrames

julia> vars = matread("test.mat"); table=DataFrame)

julia> vars["s"]["subtable"][2]
2×2 DataFrame
 Row │ x        y       
     │ Float64  Float64
─────┼──────────────────
   1 │    21.0     23.0
   2 │    22.0     24.0

The only non-supported case for DataFrame is ND-arrays inside table columns, but I have the default MAT.MatlabTable type as fallback.

matthijscox avatar Nov 20 '25 14:11 matthijscox