JuliaIO / MAT.jl

Julia module for reading MATLAB files
MIT License
280 stars 71 forks source link

matwrite fails for a basic dictionary on an isbits test #162

Open BambOoxX opened 3 years ago

BambOoxX commented 3 years ago

MAT.jl v0.10.1 seems to fail with this call

D = Dict("num" => rand(),
              "vec" => rand(2),
              "mat" => rand(3, 3),
              "str" => "abc",
              "chr" => 'A',
               )
fname = tempname()*".mat"
matwrite(fname,D

is there something brroken, or I am using this incorrectly ?

inkydragon commented 2 years ago

It looks like MAT.jl doesn't support writing char to mat files yet.

julia> matwrite("char.mat", Dict("chr" => 'a'))
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.File, name::String, s::Char)
   @ MAT.MAT_HDF5 C:\Users\woclass\.julia\packages\MAT\f523T\src\MAT_HDF5.jl:530
 [3] write(parent::MAT.MAT_HDF5.MatlabHDF5File, name::String, thing::Char)
   @ MAT.MAT_HDF5 C:\Users\woclass\.julia\packages\MAT\f523T\src\MAT_HDF5.jl:546
 [4] matwrite(filename::String, dict::Dict{String, Char}; compress::Bool)
   @ MAT C:\Users\woclass\.julia\packages\MAT\f523T\src\MAT.jl:157
 [5] matwrite(filename::String, dict::Dict{String, Char})
   @ MAT C:\Users\woclass\.julia\packages\MAT\f523T\src\MAT.jl:148
 [6] top-level scope
   @ REPL[43]:1
BambOoxX commented 2 years ago

Coming back to this again, the surprising part of it is that when reading a single character string, MAT automatically parses this as a Char...

julia>D = Dict("num" => rand(),
              "vec" => rand(2),
              "mat" => rand(3, 3),
              "str" => "abc",
              "chr" => "A",
             )
Dict{String, Any} with 5 entries:
  "mat" => [0.183045 0.573825 0.506827; 0.27681 0.500612 0.790263; 0.833868 0.144452 0.111698]
  "str" => "abc"
  "vec" => [0.0367621, 0.740946]
  "chr" => "A"
  "num" => 0.0702267

julia> matwrite(fname,D)

julia> matread(fname)
Dict{String, Any} with 5 entries:
  "mat" => [0.183045 0.573825 0.506827; 0.27681 0.500612 0.790263; 0.833868 0.144452 0.111698]
  "str" => "abc"
  "vec" => [0.0367621, 0.740946]
  "chr" => 'A'
  "num" => 0.0702267

julia> typeof(ans["chr"])
Char
BambOoxX commented 2 years ago

I know this is far from being good, but whouldn't it be relevant to avoid this error with Char variables by simply doing e.g.

MAT.MAT_HDF5.m_write(mfile::MAT.MAT_HDF5.MatlabHDF5File, parent::MAT.MAT_HDF5.HDF5Parent, name::String, char::AbstractChar) = MAT.MAT_HDF5.m_write(mfile, parent, name, string(char))

This now results in a correct roundtrip from/to .mat files

This would allow to close #143 as well.

@simonster, what do you make of this ?

BambOoxX commented 6 months ago

Bump ?