diff --git a/src/MAT_HDF5.jl b/src/MAT_HDF5.jl index 00d2f54..b0d36d6 100644 --- a/src/MAT_HDF5.jl +++ b/src/MAT_HDF5.jl @@ -371,8 +371,19 @@ function m_writearray(parent::HDF5Parent, name::String, adata::AbstractArray{Com end end +function _normalize_arr(x) + if x isa Array + x + elseif x isa AbstractArray + collect(x) + else + x + end +end + # Write a scalar or array -function m_write(mfile::MatlabHDF5File, parent::HDF5Parent, name::String, data::Union{T, Complex{T}, Array{T}, Array{Complex{T}}}) where {T<:HDF5BitsOrBool} +function m_write(mfile::MatlabHDF5File, parent::HDF5Parent, name::String, data::Union{T, Complex{T}, AbstractArray{T}, AbstractArray{Complex{T}}}) where {T<:HDF5BitsOrBool} + data = _normalize_arr(data) if isempty(data) m_writeempty(parent, name, data) return @@ -443,7 +454,8 @@ function m_write(mfile::MatlabHDF5File, parent::HDF5Parent, name::String, str::A end # Write cell arrays -function m_write(mfile::MatlabHDF5File, parent::HDF5Parent, name::String, data::Array{T}) where T +function m_write(mfile::MatlabHDF5File, parent::HDF5Parent, name::String, data::AbstractArray{T}) where T + data = _normalize_arr(data) pathrefs = "/#refs#" fid = HDF5.file(parent) local g diff --git a/test/write.jl b/test/write.jl index 3a1d820..ae4a2eb 100644 --- a/test/write.jl +++ b/test/write.jl @@ -129,3 +129,10 @@ test_write(sd) # note: compression is NOT effective when the dict contains many duplicate entries # which are not compressible by themselves! test_compression_effective(Dict("data" => fill(1.0, 1000))) + +# test adjoint/reshape array +test_write(Dict("adjoint_arr"=>[1 2 3;4 5 6;7 8 9]')) +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))) \ No newline at end of file