bugGNU Octave - Bugs: bug #51633, Empty indexing of struct returns...

 
 

bug #51633: Empty indexing of struct returns empty struct array

Submitted by:  Piotr Held <jsoh425>
Submitted on:  Mon 31 Jul 2017 07:05:59 PM UTC  
 
Category:  Interpreter Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Matlab Compatibility
Status:  Fixed Assigned to:  None
Originator Name:  Piotr Held Open/Closed:  Closed
Release:  dev Operating System:  Any

Add a New Comment (Rich Markup)
   

You are not logged in

Please log in, so followups can be emailed to you.

 

( Jump to the original submission)

Fri 11 Aug 2017 03:26:04 PM UTC, comment #10: 

This was a simple fix.  Unlike other data types, the struct do_index_op function was not checking the length of the index list for the special case where it was 0 (empty).  I changed that and added a test for it here http://hg.savannah.gnu.org/hgweb/octave/rev/1b232c0c19e6.

I also checked the other data types and they seem to work, except possibly for objects where the subsref routine is often overloaded.  I'll ask a question about that on the Maintainer's list.

Rik <rik5>
Project Administrator
Mon 07 Aug 2017 03:51:53 PM UTC, comment #9: 

Marking as confirmed.  The issue seems to be that an empty indexing expression for structs is interpreted as indexing with a null array.

Sample Code:

octave:9> s = struct ("x", 1:10)
s =

  scalar structure containing the fields:

    x =

        1    2    3    4    5    6    7    8    9   10


octave:10> s()
ans =

  0x0 struct array containing the fields:

    x

octave:11> s([])
ans =

  0x0 struct array containing the fields:

    x

octave:12> s(1)
ans =

  scalar structure containing the fields:

    x =

        1    2    3    4    5    6    7    8    9   10

Conversely, for Arrays no index expression means perform no indexing operation at all.

octave:13> r = [1:10]
r =

    1    2    3    4    5    6    7    8    9   10

octave:14> r()
ans =

    1    2    3    4    5    6    7    8    9   10

octave:15> r([])
ans = [](0x0)
octave:16> r(1)
ans =  1

Rik <rik5>
Project Administrator
Wed 02 Aug 2017 06:31:34 PM UTC, comment #8: 

for reference, Matlab 2017 output for Dan's comment #6:

>> a = 1:10;
>> a()
ans =
     1     2     3     4     5     6     7     8     9    10
>> b(1) = struct('x',1:10)
b =
  struct with fields:

    x: [1 2 3 4 5 6 7 8 9 10]
>> b(2) = struct('x',1:10)
b =
  1×2 struct array with fields:
    x
>> b
b =
  1×2 struct array with fields:
    x
>> b()
ans =
  1×2 struct array with fields:
    x
>> b(1)
ans =
  struct with fields:

    x: [1 2 3 4 5 6 7 8 9 10]

Nicholas Jankowski <nrjank>
Tue 01 Aug 2017 10:26:17 PM UTC, comment #7: 

jwe: I'm sorry my reponse wasn't more precise.

I agree with Dan Sebald 'x()' should be the same as 'x'.

To be more precise we have:

a = 1:10;
str = 'abcd';
myints = int32(1:10);
b(1) = struct ('x', 1:10);
b(2) = struct ('x', 11:20);
flag(1) = isequal (a,a());
flag(2) = isequal (str,str());
flag(3) = isequal (myints,myints());
flag(4) = isequal (b,b());
assert (all (flag));

So in Matlab assert passes and:

flag == [1 1 1 1];

in Octave it fails and:

flag == [1 1 1 0];

Piotr Held <jsoh425>
Tue 01 Aug 2017 07:09:59 PM UTC, comment #6: 

It sounds to me like x() should always be the same as plain x.  In Octave,

octave:13> a = 1:10;
octave:14> a()
ans =

    1    2    3    4    5    6    7    8    9   10

octave:18> b(1) = struct('x',1:10);
octave:19> b(2) = struct('x',1:10);
octave:20> b
b =

  1x2 struct array containing the fields:

    x

octave:21> b() % not the same as b
ans =

  0x0 struct array containing the fields:

    x

octave:22> b(1)
ans =

  scalar structure containing the fields:

    x =

        1    2    3    4    5    6    7    8    9   10

In the vector case, 'a()' is the same as 'a'... not 'a(1)'.  So for consistency, I would think that 'b()' should print out whatever 'b' prints out--which isn't necessarily the same as what 'b(1)' prints out.

Dan Sebald <sebald>
Tue 01 Aug 2017 06:51:45 PM UTC, comment #5: 

Sorry, I'm just really confused now by the description of what Matlab is doing.

John W. Eaton <jwe>
Project Administrator
Tue 01 Aug 2017 12:33:41 AM UTC, comment #4: 

It is true of double, char, int32, classdef. So

c = 'c';
class1 = class1_constr;
my_int = int32(32);
d = 24;

All work, both in Matlab and in Octave. As far as I can tell only struct doesn't.

Piotr Held <jsoh425>
Mon 31 Jul 2017 11:24:06 PM UTC, comment #3: 

Is this true of all structs or just scalar structs?

Is this true of all scalar objects?

John W. Eaton <jwe>
Project Administrator
Mon 31 Jul 2017 08:45:01 PM UTC, comment #2: 

I mean that for Matlab the code would execute fine. What I meant by the equivalency is that:

a() == a(1)

for Matlab.

And for Octave it isn't, as your example clearly shows.

Piotr Held <jsoh425>
Mon 31 Jul 2017 08:33:57 PM UTC, comment #1: 

I'm not following what is meant by "For Matlab it is equivalent to a(1)".  "a()" in Octave is the same as "a(1)" in Matlab?  Is the following what you mean?

octave:40> a = struct ('f', 1);
octave:41> b = a()
b =

  0x0 struct array containing the fields:

    f

octave:42> b.f = 3
error: invalid assignment to cs-list outside multiple assignment
octave:42> b(1).f = 3
b =

  scalar structure containing the fields:

    f =  3

octave:43>

Dan Sebald <sebald>
Mon 31 Jul 2017 07:05:59 PM UTC, original submission:  

I apologize if this has been submitted earlier, I couldn't find it.

So with this code:

a = struct ('f', 1);
a()

Returns

0x0 struct array containing the fields:
    f

For Matlab it is equivalent to:

a(1)

This might seem trivial in most cases, but if you had a class name 'class1' and property name 'prop1'. If 'prop1' happens to be a struct your (somewhat improper) code:

sth = class1.prop1();
sth.field1 = 5;

Fails when you try to assign something to a field of that struct.

Should I try to fix this, or is the code example I gave just "bad coding" and it's better if the code doesn't execute?

Piotr Held <jsoh425>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by nrjank (Posted a comment)
  • -email is unavailable- added by jwe (Posted a comment)
  • -email is unavailable- added by sebald (Posted a comment)
  • -email is unavailable- added by jsoh425 (Submitted the item)
  •  

    Do you think this task is very important?
    If so, you can add your encouragement to it.
    This task has 0 encouragements so far.

    Only project members can vote.

     

     

     

    Follow 3 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2017-08-11 rik5 StatusConfirmed => Fixed
        Open/ClosedOpen => Closed
    2017-08-07 rik5 StatusNone => Confirmed

    Back to the top


    Powered by Savane 3.5