Skip to content

Commit 67900a7

Browse files
committed
MatlabStructArray integer indexing
1 parent ad6c8fb commit 67900a7

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/MAT_types.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,16 @@ function Base.Array{D,N}(arr::MatlabStructArray{N}) where {D<:AbstractDict,N}
258258
return result
259259
end
260260

261+
function Base.getindex(arr::MatlabStructArray, s::Integer)
262+
row_values = [getindex(v, s) for v in arr.values]
263+
if isempty(arr.class)
264+
D = Dict{String,Any}
265+
else
266+
D = OrderedDict{String,Any}
267+
end
268+
return create_struct(D, arr.names, row_values, arr.class)
269+
end
270+
261271
function create_struct(::Type{D}, keys, values, class::String) where {T,D<:AbstractDict{T}}
262272
return D(T.(keys) .=> values)
263273
end

test/types.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,28 @@ using Dates
7878
@test_throws ErrorException(msg) MatlabStructArray(wrong_sarr)
7979
end
8080

81+
@testset "MatlabStructArray integer indexing" begin
82+
d_arr = Dict{String, Any}[
83+
Dict("x"=>[1.0,2.0], SubString("y")=>3.0),
84+
Dict("x"=>[5.0,6.0], "y"=>[])
85+
]
86+
s_arr = MatlabStructArray(d_arr)
87+
@test s_arr[1] == Array(s_arr)[1]
88+
@test s_arr[2] == Array(s_arr)[2]
89+
90+
s_arr = MatlabStructArray(reshape(d_arr, 1, 2))
91+
@test s_arr[1] == Array(s_arr)[1]
92+
@test s_arr[2] == Array(s_arr)[2]
93+
94+
s_arr = MatlabStructArray(reshape(d_arr, 2, 1))
95+
@test s_arr[1] == Array(s_arr)[1]
96+
@test s_arr[2] == Array(s_arr)[2]
97+
98+
s_arr = MatlabStructArray(reshape(d_arr, 2, 1), "TestClass")
99+
@test s_arr[1] == Array(s_arr)[1]
100+
@test s_arr[2] == Array(s_arr)[2]
101+
end
102+
81103
@testset "MatlabClassObject" begin
82104
d = Dict{String,Any}("a" => 5)
83105
obj = MatlabClassObject(d, "TestClassOld")

0 commit comments

Comments
 (0)