Use MAT.jl to save a .mat file containing a MATLAB struct array

Does anyone know if it’s possible to use MAT.jl to write a .mat file that contains a MATLAB struct array? It would be nice if something like this worked:

julia> using MAT

julia> struct A
           x::Int
           y::Int
       end

julia> matwrite("test.mat", Dict("x" => [A(1, 2), A(3, 4)]))
ERROR: This is the write function for CompositeKind, but the input doesn't fit
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:33
 [2] m_write(mfile::MAT.MAT_HDF5.MatlabHDF5File, parent::HDF5.Group, name::String, s::A)
   @ MAT.MAT_HDF5 ~/.julia/packages/MAT/f523T/src/MAT_HDF5.jl:530
 [3] m_write(mfile::MAT.MAT_HDF5.MatlabHDF5File, parent::HDF5.File, name::String, data::Vector{A})
   @ MAT.MAT_HDF5 ~/.julia/packages/MAT/f523T/src/MAT_HDF5.jl:479
 [4] write(parent::MAT.MAT_HDF5.MatlabHDF5File, name::String, thing::Vector{A})
   @ MAT.MAT_HDF5 ~/.julia/packages/MAT/f523T/src/MAT_HDF5.jl:546
 [5] matwrite(filename::String, dict::Dict{String, Vector{A}}; compress::Bool)
   @ MAT ~/.julia/packages/MAT/f523T/src/MAT.jl:157
 [6] matwrite(filename::String, dict::Dict{String, Vector{A}})
   @ MAT ~/.julia/packages/MAT/f523T/src/MAT.jl:148
 [7] top-level scope
   @ REPL[14]:1

It looks like writing a vector of named tuples creates a MATLAB cell array of (scalar) structs, so I guess I can work with that.

Julia:

matwrite(
    "test.mat",
    Dict("x" => [(a = 1, b = 3.4, c = "hello"),
                 (a = 2, b = 1.1, c = "world")])
)

Matlab:

>> x

x =

  2×1 cell array

    {1×1 struct}
    {1×1 struct}

>> x{1}

ans = 

  struct with fields:

    a: 1
    b: 3.4000
    c: 'hello'
1 Like