Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
16 changes: 13 additions & 3 deletions src/MAT_v5.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions test/read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Binary file added test/v7/varnames.mat
Binary file not shown.
Loading