Trouble reading numerical data out of a MAT file

I have a mat file that was created using Julia. It contains a variable T that stores a 360x180 matrix of numerical values that was generated using DIVAnd.jl. I can read the values out using Matlab without any hiccup at all.

Here is the code to read the mat file:

# Read values out of the mat file:
using MAT;
F1 = "path/test.mat";
T   = read(F1, "T");
close(F1);

Here are the errors I got when trying to open it using Julia:

ERROR: LoadError: MethodError: no method matching read(::IOStream, ::String)
Closest candidates are:
  read(::Union{HDF5.File, HDF5.Group}, ::AbstractString; pv...) at ~/.julia/packages/HDF5/pIJra/src/HDF5.jl:1159
  read(::AbstractString, ::Any...) at /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/base/io.jl:434
  read(::MAT.MAT_v5.Matlabv5File, ::String) at ~/.julia/packages/MAT/f523T/src/MAT_v5.jl:407
  ...
Stacktrace:
 [1] (::Base.var"#362#363"{Tuple{String}})(io::IOStream)
   @ Base ./io.jl:434
 [2] open(f::Base.var"#362#363"{Tuple{String}}, args::String; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ Base ./io.jl:330
 [3] open(f::Function, args::String)
   @ Base ./io.jl:328
 [4] read(filename::String, args::String)
   @ Base ./io.jl:434
 [5] top-level scope

Here is how the mat file was created:

matwrite(F3, Dict(
"T"   => T,
"S"  => S
); compress = true)

Many thanks!

it looks like the usage should be:

using MAT;
F1 = matopen("path/test.mat")
T   = read(F1, "T")
close(F1)

your original code doesn’t make sense because you’re close()ing a string and you don’t need to / can’t do that.

2 Likes

Many thanks! It is working great now.

Can’t imagine I made that mistake.