NeurodataWithoutBorders / matnwb

A Matlab interface for reading and writing NWB files
BSD 2-Clause "Simplified" License
49 stars 32 forks source link

Error loading rows in dynamic table #286

Closed ryaoki closed 3 years ago

ryaoki commented 3 years ago

When I tried to load 2nd or later rows in dynamic tables with types.util.dynamictable.getRow, it returned the data with the last element of the previous row. Here is the snippet to exemplify the problem.

clear;
generateCore();

% generate the table
dynTable = types.hdmf_common.DynamicTable(...
    'colnames', {'foo','goo'},...
    'description', 'aaa',...
    'id', types.hdmf_common.ElementIdentifiers(...
    'data', []),...
    'foo', types.hdmf_common.VectorData('description', 'foo'),...
    'foo_index', types.hdmf_common.VectorIndex('target', types.untyped.ObjectView('/dynTable/foo')),...
    'goo', types.hdmf_common.VectorData('description', 'goo'),...
    'goo_index', types.hdmf_common.VectorIndex('target', types.untyped.ObjectView('/dynTable/goo')));

% add rows
for i = 1:10
    dynTable.addRow(...
        'id', i,...
        'foo', i,...
        'goo', (1:i)')
end

% get the 3rd row
dat = dynTable.getRow(3, 'useID', true', 'columns', {'foo','goo'})
dat =  1×2 table

      foo             goo     
  ____________    ____________

  {2×1 double}    {4×1 double}

dat.foo{1}
dat.goo{1}

ans =

     2
     3

ans =

     2
     1
     2
     3

The expected output should be

dat =

    1×2 table

          foo             goo     
      ____________    ____________

      {1×1 double}    {3×1 double}

and dat.foo{1} = 3, dat.goo{1} = [1; 2; 3]

Here, the 3rd row was loaded and "foo" should be 3 and "goo" should be [1; 2; 3], but both have an extra 2 at the beginning.

This seems to be happining because the types.util.dynamictable.getRow.m is written as 0-index. Here is an idea of fixing that https://github.com/ryaoki/matnwb/commit/2b24e480dcee59a197959e8c98ab2589577d744d

lawrence-mbf commented 3 years ago

As it turns out, I fixed this in #284 but never cherry-picked it over. Thanks for pointing this out!

lawrence-mbf commented 3 years ago

Ah, sorry I should ask you first. Does the tip of master work for you?

ryaoki commented 3 years ago

It worked perfectly! Thanks a lot!