Skip to content

Commit 98e91a9

Browse files
ZhuWenhao-88宋家豪
andauthored
Support saving AbstractArray as mat file (#203)
* Support saving AbstractArray as mat file * Improve the test case * add gitlab ci * Support saving AbstractArray as mat file * Improve the test case * remove unexpected file --------- Co-authored-by: 宋家豪 <[email protected]>
1 parent 926dbd9 commit 98e91a9

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/MAT_HDF5.jl

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,19 @@ function m_writearray(parent::HDF5Parent, name::String, adata::AbstractArray{Com
371371
end
372372
end
373373

374+
function _normalize_arr(x)
375+
if x isa Array
376+
x
377+
elseif x isa AbstractArray
378+
collect(x)
379+
else
380+
x
381+
end
382+
end
383+
374384
# Write a scalar or array
375-
function m_write(mfile::MatlabHDF5File, parent::HDF5Parent, name::String, data::Union{T, Complex{T}, Array{T}, Array{Complex{T}}}) where {T<:HDF5BitsOrBool}
385+
function m_write(mfile::MatlabHDF5File, parent::HDF5Parent, name::String, data::Union{T, Complex{T}, AbstractArray{T}, AbstractArray{Complex{T}}}) where {T<:HDF5BitsOrBool}
386+
data = _normalize_arr(data)
376387
if isempty(data)
377388
m_writeempty(parent, name, data)
378389
return
@@ -443,7 +454,8 @@ function m_write(mfile::MatlabHDF5File, parent::HDF5Parent, name::String, str::A
443454
end
444455

445456
# Write cell arrays
446-
function m_write(mfile::MatlabHDF5File, parent::HDF5Parent, name::String, data::Array{T}) where T
457+
function m_write(mfile::MatlabHDF5File, parent::HDF5Parent, name::String, data::AbstractArray{T}) where T
458+
data = _normalize_arr(data)
447459
pathrefs = "/#refs#"
448460
fid = HDF5.file(parent)
449461
local g

test/write.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,10 @@ test_write(sd)
129129
# note: compression is NOT effective when the dict contains many duplicate entries
130130
# which are not compressible by themselves!
131131
test_compression_effective(Dict("data" => fill(1.0, 1000)))
132+
133+
# test adjoint/reshape array
134+
test_write(Dict("adjoint_arr"=>[1 2 3;4 5 6;7 8 9]'))
135+
test_write(Dict("reshape_arr"=>reshape([1 2 3;4 5 6;7 8 9]',1,9)))
136+
137+
test_write(Dict("adjoint_arr"=>Any[1 2 3;4 5 6;7 8 9]'))
138+
test_write(Dict("reshape_arr"=>reshape(Any[1 2 3;4 5 6;7 8 9]',1,9)))

0 commit comments

Comments
 (0)