diff --git a/Project.toml b/Project.toml index 23c392f..3e1f641 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "MAT" uuid = "23992714-dd62-5051-b70f-ba57cb901cac" -version = "0.12.0" +version = "0.12.1" [deps] CodecZlib = "944b1d66-785c-5afd-91f1-9de20f533193" diff --git a/src/MAT_v5.jl b/src/MAT_v5.jl index b25237e..084c8c4 100644 --- a/src/MAT_v5.jl +++ b/src/MAT_v5.jl @@ -441,10 +441,20 @@ function getvarnames(matfile::Matlabv5File) error("Unexpected data type") end - read_element(f, matfile.swap_bytes, UInt32) - read_element(f, matfile.swap_bytes, Int32) - varnames[String(read_element(f, matfile.swap_bytes, UInt8))] = offset + flags = read_element(f, matfile.swap_bytes, UInt32) + class = flags[1] & 0xFF + if class != mxOPAQUE_CLASS + read_element(f, matfile.swap_bytes, Int32) + end + + varname = String(read_element(f, matfile.swap_bytes, UInt8)) + if varname == "" + # Skip unnamed variable (subsystem) + seek(matfile.ios, offset+nbytes+hbytes) + continue + end + varnames[varname] = offset seek(matfile.ios, offset+nbytes+hbytes) end end diff --git a/test/read.jl b/test/read.jl index 0b2ab64..5f7b8ba 100644 --- a/test/read.jl +++ b/test/read.jl @@ -418,3 +418,29 @@ let objtestfile = "char_array_old_null.mat" @test haskey(vars, "simple_string") @test vars["simple_string"] == "t\0e\0q\0i\0k\0b\0o\0n\0f\0x" end + +@testset "read variable names" begin + let objtestfile = "varnames.mat" + file = matopen(joinpath(dirname(@__FILE__), "v7", objtestfile)) + var_name_dict = keys(file) + @test Set(var_name_dict) == Set(["a", "b", "c", "d"]) + + @test haskey(file, "a") + @test haskey(file, "b") + @test haskey(file, "c") + @test haskey(file, "d") + @test !haskey(file, "e") + + data = read(file, "a") + @test data == reshape([1.0; 2.0; 3.0], 3, 1) + + data = read(file, "b") + @test data == "hello" + + data = read(file, "c") + @test data == "stringstring" + + data = read(file, "d") + @test data == reshape([4.0, 5.0, 6.0], 3, 1) + end +end diff --git a/test/v7/varnames.mat b/test/v7/varnames.mat new file mode 100644 index 0000000..7f5e765 Binary files /dev/null and b/test/v7/varnames.mat differ