bugGNU Octave - Bugs: bug #32863, x.y.z syntax for structure fails...

 
 

bug #32863: x.y.z syntax for structure fails ("scalar cannot be indexed with .")

Submitted by:  None
Submitted on:  Sun 20 Mar 2011 11:34:55 AM UTC  
 
Category:  None Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Documentation
Status:  Invalid Assigned to:  None
Originator Name:  Jonathan Schachter Originator Email:  -email is unavailable-
Open/Closed:  Closed Release:  3.2.4
Operating System:  Microsoft Windows

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)

Thu 24 Mar 2011 12:21:54 AM UTC, comment #6: 

If you're goal was to mimic a hash-like structure you can arrange to do that by careful ordering of assignments.  See below.

tmp_struct.a = 1;
tmp_struct.b = 2;
x.label1 = tmp_struct;
tmp_struct.a = 3;
tmp_struct.b = 4;
x.label2 = tmp_struct;
x
x =

  scalar structure containing the fields:

    label1 =

      scalar structure containing the fields:

        a =  1
        b =  2

    label2 =

      scalar structure containing the fields:

        a =  3
        b =  4

Also, you might want to be aware of the () notation which allows you to use variables in place of explicit labels.  For example,

label = 'hello';
a.(label) = 'world';
a =

  scalar structure containing the fields:

    hello = world

Rik <rik5>
Project Administrator
Wed 23 Mar 2011 09:04:30 AM UTC, comment #5: 

And I agree with that behavior.

Marco

Marco Caliari <caliari>
Project Member
Tue 22 Mar 2011 08:30:26 PM UTC, comment #4: 

The suggested behavior from Matlab seems to be the same for Octave.

x.y = 1;
x.y.z = 2;
# This produces error as it would overwrite x.y
x.y = [];
x.y.z = 2
# This is fine, because x.y is empty

Rik <rik5>
Project Administrator
Tue 22 Mar 2011 07:50:29 AM UTC, comment #3: 

After the warning, I get

See MATLAB 7.0.4 Release Notes, Assigning Nonstructure Variables As Structures Displays Warning for details.

and clicking on it

---------------
signing to a nonstructure variable as if it were a structure is not recommended in MATLAB. For example, if variable x holds a double (as shown below), then attempting to add a fieldname to it, thus converting x to a structure, is not good programming practice and should generate an error.
x = 10;
x.name = magic(3);

Note that if x were empty (i.e., x == []), then assigning a field to it as if it were already a structure is acceptable.
--------------

I'm using 7.6.0.

Marco Caliari <caliari>
Project Member
Mon 21 Mar 2011 08:27:33 PM UTC, comment #2: 

Can you point out where exactly in the documentation it says that you can combine the two statements as shown?  I did a quick look and I couldn't find it.

Each statement is valid by itself, but they cannot follow each other without clobbering the first assignment.  The warning message from Matlab is clearer in this regard.

As a related example,

x = 0;
y = 1/x;

are both valid assignments, but they can't be combined without generating an error.

Rik <rik5>
Project Administrator
Mon 21 Mar 2011 08:44:38 AM UTC, comment #1: 

I confirm this behaviour for 3.4.0 and 3.2.4, while it works in 3.0.5. In ml 7.6.0 it works, but you get the warning

Warning: Struct field assignment overwrites a value with class "double".
See MATLAB 7.0.4 Release Notes, Assigning Nonstructure Variables As Structures Displays Warning for details.

Marco

Marco Caliari <caliari>
Project Member
Sun 20 Mar 2011 11:34:55 AM UTC, original submission:  

Either the documentation for structures in the manual is wrong or there is a bug in the way to create structures of structures.  The docs say that both of the below are kosher:

x.y = 1
x.y.z = 2

While the first assignment suceeds, the second gives this error:

   error: scalar cannot be indexed with .

The relevant section of the docs is "6.1.1 Basic Usage and Examples."

Anonymous

 

(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 caliari (Posted a comment)
  • -email is unavailable- added by None (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
    2011-03-24 rik5 StatusNeed Info => Invalid
        Open/ClosedOpen => Closed
    2011-03-21 rik5 StatusNone => Need Info

    Back to the top


    Powered by Savane 3.5