Skip to content

Commit b71cce6

Browse files
authored
Symbol and Tuple support (JuliaIO#220)
1 parent 5c9d9b7 commit b71cce6

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

src/MAT_HDF5.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,16 @@ function m_write(mfile::MatlabHDF5File, parent::HDF5Parent, name::String, c::Abs
552552
m_write(mfile, parent, name, string(c))
553553
end
554554

555+
# Tuple
556+
function m_write(mfile::MatlabHDF5File, parent::HDF5Parent, name::String, t::Tuple)
557+
m_write(mfile, parent, name, [x for x in t])
558+
end
559+
560+
# Symbol
561+
function m_write(mfile::MatlabHDF5File, parent::HDF5Parent, name::String, s::Symbol)
562+
m_write(mfile, parent, name, string(s))
563+
end
564+
555565
# Write cell arrays
556566
function m_write(mfile::MatlabHDF5File, parent::HDF5Parent, name::String, data::AbstractArray{T}, object_decode::UInt32=UInt32(0)) where T
557567
data = _normalize_arr(data)

test/write.jl

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using MAT, Test, Dates
2+
using SparseArrays, LinearAlgebra
23

34
tmpfile = string(tempname(), ".mat")
45

@@ -150,12 +151,33 @@ test_write(Dict("reshape_arr"=>reshape([1 2 3;4 5 6;7 8 9]',1,9)))
150151
test_write(Dict("adjoint_arr"=>Any[1 2 3;4 5 6;7 8 9]'))
151152
test_write(Dict("reshape_arr"=>reshape(Any[1 2 3;4 5 6;7 8 9]',1,9)))
152153

153-
# named tuple
154-
nt = (x = 5, y = Any[6, "string"])
155-
matwrite(tmpfile, Dict("nt" => nt))
156-
nt_read = matread(tmpfile)["nt"]
157-
@test nt_read["x"] == 5
158-
@test nt_read["y"] == nt.y
154+
@testset "named tuple" begin
155+
nt = (x = 5, y = Any[6, "string"])
156+
matwrite(tmpfile, Dict("nt" => nt))
157+
nt_read = matread(tmpfile)["nt"]
158+
@test nt_read["x"] == 5
159+
@test nt_read["y"] == nt.y
160+
end
161+
162+
@testset "tuple" begin
163+
# NTuple{T}
164+
t = (5, 6)
165+
matwrite(tmpfile, Dict("t" => (5, 6)))
166+
r = matread(tmpfile)["t"]
167+
@test r == [x for x in t]
168+
169+
# otherwise cell array
170+
t = (5, "string")
171+
matwrite(tmpfile, Dict("t" => t))
172+
r = matread(tmpfile)["t"]
173+
@test r == [x for x in t]
174+
end
175+
176+
@testset "symbol" begin
177+
matwrite(tmpfile, Dict("s" => :symbol))
178+
r = matread(tmpfile)["s"]
179+
@test r == "symbol"
180+
end
159181

160182
# test nested struct array - interface via Dict array
161183
@testset "MatlabStructArray writing" begin

0 commit comments

Comments
 (0)