From bc37854ef58c060cc68d231606df5ba2e778b550 Mon Sep 17 00:00:00 2001 From: Matthijs Cox Date: Wed, 3 Dec 2025 12:02:40 +0100 Subject: [PATCH] Symbol and Tuple support --- src/MAT_HDF5.jl | 10 ++++++++++ test/write.jl | 34 ++++++++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/MAT_HDF5.jl b/src/MAT_HDF5.jl index 711e39a..46da353 100644 --- a/src/MAT_HDF5.jl +++ b/src/MAT_HDF5.jl @@ -552,6 +552,16 @@ function m_write(mfile::MatlabHDF5File, parent::HDF5Parent, name::String, c::Abs m_write(mfile, parent, name, string(c)) end +# Tuple +function m_write(mfile::MatlabHDF5File, parent::HDF5Parent, name::String, t::Tuple) + m_write(mfile, parent, name, [x for x in t]) +end + +# Symbol +function m_write(mfile::MatlabHDF5File, parent::HDF5Parent, name::String, s::Symbol) + m_write(mfile, parent, name, string(s)) +end + # Write cell arrays function m_write(mfile::MatlabHDF5File, parent::HDF5Parent, name::String, data::AbstractArray{T}, object_decode::UInt32=UInt32(0)) where T data = _normalize_arr(data) diff --git a/test/write.jl b/test/write.jl index 64a0391..95eb47d 100644 --- a/test/write.jl +++ b/test/write.jl @@ -1,4 +1,5 @@ using MAT, Test, Dates +using SparseArrays, LinearAlgebra tmpfile = string(tempname(), ".mat") @@ -150,12 +151,33 @@ test_write(Dict("reshape_arr"=>reshape([1 2 3;4 5 6;7 8 9]',1,9))) test_write(Dict("adjoint_arr"=>Any[1 2 3;4 5 6;7 8 9]')) test_write(Dict("reshape_arr"=>reshape(Any[1 2 3;4 5 6;7 8 9]',1,9))) -# named tuple -nt = (x = 5, y = Any[6, "string"]) -matwrite(tmpfile, Dict("nt" => nt)) -nt_read = matread(tmpfile)["nt"] -@test nt_read["x"] == 5 -@test nt_read["y"] == nt.y +@testset "named tuple" begin + nt = (x = 5, y = Any[6, "string"]) + matwrite(tmpfile, Dict("nt" => nt)) + nt_read = matread(tmpfile)["nt"] + @test nt_read["x"] == 5 + @test nt_read["y"] == nt.y +end + +@testset "tuple" begin + # NTuple{T} + t = (5, 6) + matwrite(tmpfile, Dict("t" => (5, 6))) + r = matread(tmpfile)["t"] + @test r == [x for x in t] + + # otherwise cell array + t = (5, "string") + matwrite(tmpfile, Dict("t" => t)) + r = matread(tmpfile)["t"] + @test r == [x for x in t] +end + +@testset "symbol" begin + matwrite(tmpfile, Dict("s" => :symbol)) + r = matread(tmpfile)["s"] + @test r == "symbol" +end # test nested struct array - interface via Dict array @testset "MatlabStructArray writing" begin