MATLAB.jl icon indicating copy to clipboard operation
MATLAB.jl copied to clipboard

Error when importing empty cell array

Open MaxandreJ opened this issue 6 years ago • 3 comments

Hi,

See first answer below, I have provided a minimal working example now. The problem is with a cell array of any size containing uninitialised values.

On Julia 1.0.2 and MATLAB.jl v"0.7.0", with a 4x4 cell array MATLAB variable containing 1200001 x 1 double (4 of them, on the first row) and [] (on the other rows),

I run

file = read_matfile(filepath)
my_variable_MxArray = file[variable_name]
my_variable_julia = jvalue(my_variable_MxArray)

and get the error

ERROR: NULL pointer for MxArray.

with the stacktrace

Stacktrace:
 [1] Type at /Users/maxandrejacqueline/.julia/packages/MATLAB/ZuG4g/src/mxarray.jl:8 [inlined]
 [2] get_cell at /Users/maxandrejacqueline/.julia/packages/MATLAB/ZuG4g/src/mxarray.jl:359 [inlined]
 [3] _jarrayx(::String, ::MATLAB.MxArray, ::Tuple{Int64,Int64}) at /Users/maxandrejacqueline/.julia/packages/MATLAB/ZuG4g/src/mxarray.jl:506
 [4] jmatrix(::MATLAB.MxArray) at /Users/maxandrejacqueline/.julia/packages/MATLAB/ZuG4g/src/mxarray.jl:521
 [5] jvalue(::MATLAB.MxArray) at /Users/maxandrejacqueline/.julia/packages/MATLAB/ZuG4g/src/mxarray.jl:605

I do not have time to provide a minimal working example (MWE) at the moment. I have provided an MWE now.

Best, Max

MaxandreJ avatar Nov 17 '18 23:11 MaxandreJ

Minimal working example

Creating a variable in MATLAB using

my_test = cell(1);

which displays as

my_test =

  1×1 cell array

    {0×0 double}

and saving it

save("../../../Data/test/test.mat",'my_test')

I try to import it in Julia using

my_matfile_contents = read_matfile("../../Data/test/test.mat")

my_variable_MxArray = my_matfile_contents["my_test"]

my_variable_julia = jvalue(my_struct_MxArray)

I get the error and stacktrace

ERROR: NULL pointer for MxArray.
Stacktrace:
 [1] Type at /Users/maxandrejacqueline/.julia/packages/MATLAB/ZuG4g/src/mxarray.jl:8 [inlined]
 [2] get_cell at /Users/maxandrejacqueline/.julia/packages/MATLAB/ZuG4g/src/mxarray.jl:359 [inlined]
 [3] _jarrayx(::String, ::MxArray, ::Tuple{Int64}) at /Users/maxandrejacqueline/.julia/packages/MATLAB/ZuG4g/src/mxarray.jl:506
 [4] jvector(::MxArray) at /Users/maxandrejacqueline/.julia/packages/MATLAB/ZuG4g/src/mxarray.jl:515
 [5] jvalue(::MxArray) at /Users/maxandrejacqueline/.julia/packages/MATLAB/ZuG4g/src/mxarray.jl:605
 [6] top-level scope at none:0

.

However, if I manually initialise the values of my cell array in MATLAB, as such

my_test = cell(1);
my_test{1} = [];
save("../../../Data/test/test.mat",'my_test')

my variable still displays as

my_test =

  1×1 cell array

    {0×0 double}

but this time when I run in Julia

my_matfile_contents = read_matfile("../../Data/test/test.mat")

my_variable_MxArray = my_matfile_contents["my_test"]

my_variable_julia = jvalue(my_struct_MxArray)

I get the correct behaviour

1-element Array{Any,1}:
 Array{Float64}(0,0)

This may mean that the initialisation of cells in a cell array by the command cell(dimensions) in MATLAB is actually different from initialising the cells manually (eg. my_test{1} = [];), even if it is not shown to the user. scipy.io works with both initialisation methods and doesn't return any MatlabOpaque object so I don't think this is to do with the MATLAB C API vs MATLAB C++ API.

MaxandreJ avatar Nov 18 '18 00:11 MaxandreJ

I think I've got to the root of the problem... Using scipy.io in Julia (through PyCall) to import the variables, I've managed to find a difference between the two ways of initialising the cell array.

If I do

my_test = cell(1);

to create the cell array, I import it in Julia using scipy.io, and I inspect the variable, I get

PyObject array([], shape=(1, 0), dtype=float64)

whereas if I initialise the cell array with

my_test = cell(1);
my_test{1} = [];

I import it in Julia using scipy.io, and I inspect the variable, I get

PyObject array([], shape=(0, 0), dtype=uint8)

Notice the difference in shape and variable type! This should prove useful when debugging this issue.

Best, Max

MaxandreJ avatar Nov 18 '18 01:11 MaxandreJ

Thanks for the detailed bug report and MWE.

musm avatar Nov 20 '18 18:11 musm