How to read MAT files containing strings?

I have a MAT file containing some variables that store string vectors. Below is one example of such a variable:


When I use the MAT.jl package to read the info out, I’m encountering multiple errors. I wonder if anyone knows an alternative way of getting the strings out?

Below is my code:

using MAT
File1 = matopen("/path/test.mat");
A = read(File1, "A");

Here are the errors. My data does not contain headers. Obviously strings are making things complicated.

LoadError: zlib error: incorrect header check (code: -3)
[1] **changemode!(** stream::TranscodingStreams.TranscodingStream{CodecZlib.ZlibDecompressor, IOStream}, newmode::Symbol **)**
@ TranscodingStreams ~/.julia/packages/TranscodingStreams/IVlnc/src/stream.jl:717
[2] **callprocess(** stream::TranscodingStreams.TranscodingStream{CodecZlib.ZlibDecompressor, IOStream}, inbuf::TranscodingStreams.Buffer, outbuf::TranscodingStreams.Buffer **)**
@ TranscodingStreams ~/.julia/packages/TranscodingStreams/IVlnc/src/stream.jl:649
[3] **fillbuffer(** stream::TranscodingStreams.TranscodingStream{CodecZlib.ZlibDecompressor, IOStream}; eager::Bool **)**
@ TranscodingStreams ~/.julia/packages/TranscodingStreams/IVlnc/src/stream.jl:577
[4] **fillbuffer**
@ ~/.julia/packages/TranscodingStreams/IVlnc/src/stream.jl:564 [inlined]
[5] **eof(** stream::TranscodingStreams.TranscodingStream{CodecZlib.ZlibDecompressor, IOStream} **)**
@ TranscodingStreams ~/.julia/packages/TranscodingStreams/IVlnc/src/stream.jl:188
[6] **unsafe_read(** stream::TranscodingStreams.TranscodingStream{CodecZlib.ZlibDecompressor, IOStream}, output::Ptr{UInt8}, nbytes::UInt64 **)**
@ TranscodingStreams ~/.julia/packages/TranscodingStreams/IVlnc/src/stream.jl:356
[7] **unsafe_read**
@ ./io.jl:724 [inlined]
[8] **read!**
@ ./io.jl:736 [inlined]
[9] **read_bswap(** f::TranscodingStreams.TranscodingStream{CodecZlib.ZlibDecompressor, IOStream}, swap_bytes::Bool, #unused#::Type{UInt8}, dim::Int64 **)**
@ MAT.MAT_v5 ~/.julia/packages/MAT/f523T/src/MAT_v5.jl:86
[10] **read_element(** f::TranscodingStreams.TranscodingStream{CodecZlib.ZlibDecompressor, IOStream}, swap_bytes::Bool, #unused#::Type{UInt8} **)**
@ MAT.MAT_v5 ~/.julia/packages/MAT/f523T/src/MAT_v5.jl:124
[11] **getvarnames(** matfile::MAT.MAT_v5.Matlabv5File **)**
@ MAT.MAT_v5 ~/.julia/packages/MAT/f523T/src/MAT_v5.jl:393
[12] **read(** matfile::MAT.MAT_v5.Matlabv5File, varname::String **)**
@ MAT.MAT_v5 ~/.julia/packages/MAT/f523T/src/MAT_v5.jl:408

When I save your example vector in matlab using save("test.mat", "test", '-nocompression', '-v7.3') and then try to read it in julia using the current version of MAT.jl, I get the following warning:

julia> input = matread("./data/test.mat")
┌ Warning: MATLAB string values are currently not supported
└ @ MAT.MAT_HDF5 ~/.julia/packages/MAT/f523T/src/MAT_HDF5.jl:167
Dict{String, Any} with 1 entry:
  "test" => missing

So it seems this feature is currently still missing.
As far as I know .mat files are HDF5 files under the hood anyway (at least the newer versions), so you could try exporting them directly as such and then use HDF5.jl to read them.

1 Like

Many thanks for the reply. I’ll check out HDF5.