octave-bug-tracker
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Octave-bug-tracker] [bug #53906] Cannot make an object array with squar


From: Andrew Janke
Subject: [Octave-bug-tracker] [bug #53906] Cannot make an object array with square brackets
Date: Sun, 1 Jul 2018 03:40:08 -0400 (EDT)
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36

Follow-up Comment #4, bug #53906 (project octave):

> So Matlab defines a default implementation of cat/horzcat/vertcat for
classes?

Yep. It works like concatenating structs, plus the inputs are implicitly
converted to the class of the left-most, superior-most object. All the related
structural operations (repmat, ctranspose, circshift, ()-referencing, etc) are
similarly defined.

Conversely, you can dot-reference into an array of objects, and it extracts
fields into a comma-separated list.

Example:

Person class:


classdef Person
    properties
        firstName
        lastName
    end
    methods
        function this = Person(first, last)
            if nargin == 0
                return
            end
            this.firstName = first;
            this.lastName = last;
        end
    end
end


Behavior:


>> p = Person('Alice', 'Foo')
p = 
  Person with properties:

    firstName: 'Alice'
     lastName: 'Foo'
>> p2 = Person('Bob', 'Bar')
p2 = 
  Person with properties:

    firstName: 'Bob'
     lastName: 'Bar'
>> pp = [p p2]
pp = 
  1×2 Person array with properties:

    firstName
    lastName

>> pp.firstName
ans =
    'Alice'
ans =
    'Bob'
>> {pp.firstName}
ans =
  1×2 cell array
    {'Alice'}    {'Bob'}
>> pp(4) = Person('Carol','Baz')
pp = 
  1×4 Person array with properties:

    firstName
    lastName

>> pp.firstName
ans =
    'Alice'
ans =
    'Bob'
ans =
     []
ans =
    'Carol'
>> 


    _______________________________________________________

Reply to this item at:

  <http://savannah.gnu.org/bugs/?53906>

_______________________________________________
  Message sent via Savannah
  https://savannah.gnu.org/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]