Skip to content

Commit 50a9156

Browse files
authored
support setindex! for MatlabStructArray (JuliaIO#211)
1 parent 45f8231 commit 50a9156

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "MAT"
22
uuid = "23992714-dd62-5051-b70f-ba57cb901cac"
3-
version = "0.11.0"
3+
version = "0.11.1"
44

55
[deps]
66
BufferedStreams = "e1450e63-4bb3-523b-b2a4-4ffa8c0fd77d"

src/MAT_types.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,19 @@ function Base.getindex(m::MatlabStructArray, s::AbstractString)
185185
return getindex(m.values, idx)
186186
end
187187

188+
function Base.setindex!(m::MatlabStructArray{N1}, input::AbstractArray{T,N2}, s::AbstractString) where {N1,N2,T}
189+
error("size of input is not equal to MatlabStructArray column size")
190+
end
191+
192+
function Base.setindex!(m::MatlabStructArray{N}, input::AbstractArray{T,N}, s::AbstractString) where {N,T}
193+
idx = find_index(m, s)
194+
v = Array{Any,N}(input)
195+
if size(v) != size(m.values[idx])
196+
error("size of input is not equal to MatlabStructArray column size")
197+
end
198+
return setindex!(m.values, v, idx)
199+
end
200+
188201
function Base.get(m::MatlabStructArray, s::AbstractString, default)
189202
idx = findfirst(isequal(s), m.names)
190203
if isnothing(idx)

test/types.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ using Dates
4242
@test !haskey(s_arr, "wrong")
4343
@test get(s_arr, "wrong", nothing) === nothing
4444

45+
# setindexing
46+
msg = "field \"z\" not found in MatlabStructArray"
47+
@test_throws ErrorException(msg) s_arr["z"] = [1.0, 2.0]
48+
s_arr["x"] = [1.0, 2.0]
49+
@test s_arr["x"] == Any[1.0, 2.0]
50+
msg = "size of input is not equal to MatlabStructArray column size"
51+
@test_throws ErrorException(msg) s_arr["x"] = Any[1.0]
52+
@test_throws ErrorException(msg) s_arr["x"] = reshape(Any[1.0,2.0], 1, 2)
53+
4554
# possibility to convert back to dict array via `Array`
4655
s_arr = MatlabStructArray(d_arr)
4756
@test Array(s_arr) == d_arr

0 commit comments

Comments
 (0)