|
| 1 | +# Objects and struct arrays |
| 2 | + |
| 3 | +To better handle special cases we have these types since MAT 0.11: |
| 4 | +* [`MatlabStructArray`](@ref MatlabStructArray) |
| 5 | +* [`MatlabClassObject`](@ref MatlabClassObject) |
| 6 | + |
| 7 | +## Struct arrays vs Cell arrays |
| 8 | + |
| 9 | +Cell arrays are written for `Array{Any}` or any other unsupported element type: |
| 10 | + |
| 11 | +```julia |
| 12 | +sarr = Any[ |
| 13 | + Dict("x"=>1.0, "y"=>2.0), |
| 14 | + Dict("x"=>3.0, "y"=>4.0) |
| 15 | +] |
| 16 | +matwrite("matfile.mat", Dict("cell" => sarr)) |
| 17 | + |
| 18 | +``` |
| 19 | + |
| 20 | +Inside MATLAB you will find: |
| 21 | + |
| 22 | +```matlab |
| 23 | +>> load('matfile.mat') |
| 24 | +>> cell |
| 25 | +
|
| 26 | +cell = |
| 27 | +
|
| 28 | + 2×1 cell array |
| 29 | +
|
| 30 | + {1×1 struct} |
| 31 | + {1×1 struct} |
| 32 | +``` |
| 33 | + |
| 34 | +Read and write behavior for struct arrays is different. For struct arrays we use the `MatlabStructArray` type. You can also write with MAT.jl using Dict arrays `AbstractArray{<:AbstractDict}` if all the Dicts have equal keys, which will automatically convert internally to `MatlabStructArray`. |
| 35 | + |
| 36 | +```julia |
| 37 | +sarr = Dict{String, Any}[ |
| 38 | + Dict("x"=>1.0, "y"=>2.0), |
| 39 | + Dict("x"=>3.0, "y"=>4.0) |
| 40 | +] |
| 41 | +matwrite("matfile.mat", Dict("s" => sarr)) |
| 42 | +# which is the same as: |
| 43 | +matwrite("matfile.mat", Dict("s" => MatlabStructArray(sarr))) |
| 44 | +# which is the same as: |
| 45 | +matwrite("matfile.mat", Dict("s" => MatlabStructArray(["x", "y"], [[1.0, 3.0], [2.0, 4.0]]))) |
| 46 | +``` |
| 47 | + |
| 48 | +Now you'll find the following inside MATLAB: |
| 49 | + |
| 50 | +```matlab |
| 51 | +>> load('matfile.mat') |
| 52 | +>> s |
| 53 | +
|
| 54 | +s = |
| 55 | +
|
| 56 | +[2x1 struct, 576 bytes] |
| 57 | +x: 1 |
| 58 | +y: 2 |
| 59 | +``` |
| 60 | + |
| 61 | +Note that when you read the file again, you'll find the `MatlabStructArray`, which you can convert back to the Dict array with `Array`: |
| 62 | + |
| 63 | +```julia |
| 64 | +julia> sarr = matread("matfile.mat")["struct_array"] |
| 65 | +MatlabStructArray{1} with 2 columns: |
| 66 | + "x": Any[1.0, 3.0] |
| 67 | + "y": Any[2.0, 4.0] |
| 68 | + |
| 69 | +julia> sarr["x"] |
| 70 | +2-element Vector{Any}: |
| 71 | + 1.0 |
| 72 | + 3.0 |
| 73 | + |
| 74 | +julia> Array(sarr) |
| 75 | +2-element Vector{Dict{String, Any}}: |
| 76 | + Dict("x" => 1.0, "y" => 2.0) |
| 77 | + Dict("x" => 3.0, "y" => 4.0) |
| 78 | + |
| 79 | +``` |
| 80 | + |
| 81 | +Note that before v0.11 MAT.jl will read struct arrays as a Dict with concatenated arrays in the fields/keys, which is equal to `Dict(sarr)`. |
| 82 | + |
| 83 | +## Object Arrays |
| 84 | + |
| 85 | +You can write an old class object with the `MatlabClassObject` and arrays of objects with `MatlabStructArray` by providing the class name. These are also the types you obtain when you read files. |
| 86 | + |
| 87 | +Write a single class object: |
| 88 | +```julia |
| 89 | +d = Dict("foo" => 5.0) |
| 90 | +obj = MatlabClassObject(d, "TestClassOld") |
| 91 | +matwrite("matfile.mat", Dict("tc_old" => obj)) |
| 92 | +``` |
| 93 | + |
| 94 | +A class object array |
| 95 | +```julia |
| 96 | +class_array = MatlabStructArray(["foo"], [[5.0, "bar"]], "TestClassOld") |
| 97 | +matwrite("matfile.mat", Dict("class_array" => class_array)) |
| 98 | +``` |
| 99 | + |
| 100 | +Also a class object array, but will be converted to `MatlabStructArray` internally: |
| 101 | +```julia |
| 102 | +class_array = MatlabClassObject[ |
| 103 | + MatlabClassObject(Dict("foo" => 5.0), "TestClassOld"), |
| 104 | + MatlabClassObject(Dict("foo" => "bar"), "TestClassOld") |
| 105 | +] |
| 106 | +matwrite("matfile.mat", Dict("class_array" => class_array)) |
| 107 | +``` |
| 108 | + |
| 109 | +A cell array: |
| 110 | +```julia |
| 111 | +cell_array = Any[ |
| 112 | + MatlabClassObject(Dict("foo" => 5.0), "TestClassOld"), |
| 113 | + MatlabClassObject(Dict("a" => "bar"), "AnotherClass") |
| 114 | +] |
| 115 | +matwrite("matfile.mat", Dict("cell_array" => cell_array)) |
| 116 | +``` |
| 117 | + |
0 commit comments