# How to read MAT files containing strings?

**URL:** https://discourse.julialang.org/t/how-to-read-mat-files-containing-strings/72217
**Category:** General Usage
**Created:** [November 29, 2021, 1:27am UTC](https://discourse.julialang.org/t/how-to-read-mat-files-containing-strings/72217 "2021-11-29T01:27:04Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)
#### Post date: [November 29, 2021, 1:27am UTC](https://discourse.julialang.org/t/how-to-read-mat-files-containing-strings/72217/1 "2021-11-29T01:27:05Z")

</div>

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

```julia
    "HY0802"
    "HY0807"
    "HY0811"
    "HY0812"
    "HY0825"

```

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:

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

```

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

```julia
LoadError: zlib error: incorrect header check (code: -3)
Stacktrace:
[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

```

---

<div class="post-metadata">

### Author: ![mapi1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mapi1/32/29237_2.png) [@mapi1](https://discourse.julialang.org/u/mapi1)
#### Post date: [November 29, 2021, 8:14am UTC](https://discourse.julialang.org/t/how-to-read-mat-files-containing-strings/72217/2 "2021-11-29T08:14:57Z")

</div>

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
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](https://de.mathworks.com/help/matlab/import_export/exporting-to-hierarchical-data-format-hdf5-files.html) and then use [HDF5.jl](https://github.com/JuliaIO/HDF5.jl) to read them.

---

<div class="post-metadata">

### Author: ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)
#### Post date: [November 29, 2021, 5:15pm UTC](https://discourse.julialang.org/t/how-to-read-mat-files-containing-strings/72217/3 "2021-11-29T17:15:28Z")

</div>

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