JuliaIO / MAT.jl

Julia module for reading MATLAB files
MIT License
278 stars 76 forks source link

BitVectors of length 1 are saved as scalars not arrays #163

Open bjarthur opened 2 years ago

bjarthur commented 2 years ago
julia> using MAT

julia> matwrite("foo.mat", Dict("x"=>trues(1)))

julia> matread("foo.mat")
Dict{String, Any} with 1 entry:
  "x" => true

julia> matwrite("foo.mat", Dict("x"=>trues(2)))

julia> matread("foo.mat")
Dict{String, Any} with 1 entry:
  "x" => Bool[1, 1]

the workaround:

julia> matwrite("foo.mat", Dict("x"=>convert(Array{Any}, trues(1))))

julia> matread("foo.mat")
Dict{String, Any} with 1 entry:
  "x" => Any[true]
inkydragon commented 2 years ago

The behavior of the existing API is correct.

Generating test mat file in julia:

matwrite("ju.mat", 
    Dict(
        "x1"=>trues(1),
        "x2"=>trues(2),
        "x3"=>convert(Array{Any}, trues(1))     
))

Read the test mat file in MATLAB:

>> clear
>> load('ju.mat')
>> x1
x1 =

  logical

   1

>> x2
x2 =

  2×1 logical array

   1
   1

>> x3
x3 =

  1×1 cell array

    {[1]}

Write mat file in MATLAB:

>> clear
>> m1 = logical([1])
m1 =

  logical

   1

>> m2 = logical([1; 1])
m2 =

  2×1 logical array

   1
   1

>> m3 = logical([1 1])
m3 =

  1×2 logical array

   1   1

>> m4 = {1}
m4 =

  1×1 cell array

    {[1]}

>> m5 = {logical(1)}
m5 =

  1×1 cell array

    {[1]}

>> save('m.mat')

Read mat file in julia:

julia> matread("m.mat")
Dict{String, Any} with 5 entries:
  "m3" => Bool[1 1]
  "m2" => Bool[1; 1]
  "m4" => Any[1.0]
  "m5" => Any[true]
  "m1" => true