bugGNU Octave - Bugs: bug #53906, Cannot make an object array with...

 
 

bug #53906: Cannot make an object array with square brackets

Submitter:  Richard <crobar>
Submitted:  Tue 15 May 2018 10:34:24 AM UTC
   
 
Category:  Interpreter Severity:  2 - Minor
Priority:  3 - Low Item Group:  Matlab Compatibility
Status:  Duplicate Assigned to:  None
Originator Name:  Richard Crozier Open/Closed:  * Closed
Release:  * 5.1.0 Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Jump to the original submission

Tue 01 Dec 2020 05:00:44 PM UTC, comment #12: 

Closing as a duplicate of bug #44665.

Markus Mützel <mmuetzel>
Group administrator
Thu 15 Aug 2019 02:04:31 PM UTC, comment #11: 

Glad it's helpful!

> Maybe this function should be pulled into the Octave repos at some point...


I wouldn't endorse this. This function is a disgusting hack; IMHO if there's a change in core Octave, it should just be to fix the square bracket concatenation.

Andrew Janke <apjanke>
Wed 14 Aug 2019 08:22:12 PM UTC, comment #10: 

@Andrew: Very interesting. I will start using this function. I agree - much more convenient than cell arrays. Maybe this function should be pulled into the Octave repos at some point...

@Richard: Is Andrew's method the way you had in mind, or are there other way(s) you had in mind?

Chris <abrac>
Wed 14 Aug 2019 05:16:14 AM UTC, comment #9: 

Yep. Here's the hack I use in Packajoozle (https://github.com/apjanke/octave-packajoozle):

An "objvcat" function for "OBJect Vector conCATenation":



## -*- texinfo -*-
## @deftypefn {Function File} {@var{out} =} objvcat (@var{x}, @dots{})
##
## Concatenates object array vectors.
##
## This function just papers over the fact that Octave does not support
## the @code{[obj1 obj2]} concatenation syntax for objects.
##
## @var{x} may be an object of any type. The additional inputs may be any
## type that is assignment compatible with @var{x}, but they will typically
## just be more objects of the same type.
##
## @end deftypefn


function out = objvcat (varargin)
  # Hack to concatenate object vectorss because Octave doesn't support it as of 5.1
  #
  # The "v" in "objvcat" is for "vector" concatenation, not "vertical".
  out = [];
  for i_arg = 1:numel (varargin)
    B = varargin{i_arg};
    if isempty (out)
      out = B;
    else
      for i_B = 1:numel (B)
        out(end+1) = B(i_B);
      endfor
    endif
  endfor
endfunction


And an "objcatc" function for "OBJect conCATenation from Cells":


    function out = objcatc (c)
      %OBJCATC Hack to allow one-liners like objcatc({myobj.propname})
      mustBeA (c, "cell");
      out = objvcat (c{:});
    endfunction


Andrew Janke <apjanke>
Tue 13 Aug 2019 10:12:57 PM UTC, comment #8: 

Plus, you can actually make an array of objects, just not using square brackets.

Richard <crobar>
Tue 13 Aug 2019 10:07:22 PM UTC, comment #7: 

Not very useful, I'm afraid. Cell arrays of objects perform terribly, and you can't call methods on them directly.

Creating cell arrays of objects is not something I would recommend to a user.

Andrew Janke <apjanke>
Tue 13 Aug 2019 08:37:10 PM UTC, comment #6: 

I hope this information is not already obvious to the community already, but I have found that classdef objects work fine in cell arrays. This can be used as an alternative for now.

Perhaps this information can be suggested to the user in the warning/error when trying to create arrays classdef objects?

Chris <abrac>
Mon 25 Feb 2019 11:05:14 AM UTC, comment #5: 

see also bug #44665

Amro <amro_octave>
Sun 01 Jul 2018 07:40:08 AM UTC, comment #4: 


> 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'
>>


Andrew Janke <apjanke>
Tue 15 May 2018 06:28:20 PM UTC, comment #3: 

It works with handle classes without method overloading.

Richard <crobar>
Tue 15 May 2018 05:28:48 PM UTC, comment #2: 

So Matlab defines a default implementation of cat/horzcat/vertcat for classes? Does this only apply to value classes or can handle classes also be concatenated without any method overloading?

Mike Miller <mtmiller>
Group Member
Tue 15 May 2018 10:49:51 AM UTC, comment #1: 

cat doesn't work either:


>> z = cat (2, x, y)
warning: struct: converting a classdef object into a struct overrides the access restrictions defined for properties. All properties are returned, including private and protected ones.
error: invalid conversion of multi-dimensional struct to scalar struct



Richard <crobar>
Tue 15 May 2018 10:34:24 AM UTC, original submission:  

Making an array of classdef objects using square barckets like below does not work in octave:


>> x = method_debug_test ()
x =

<object method_debug_test>

>> y = method_debug_test ()
y =

<object method_debug_test>

>> z = [x, y]
warning: struct: converting a classdef object into a struct overrides the access restrictions defined for properties. All properties are returned, including private and protected ones.
error: invalid conversion of multi-dimensional struct to scalar struct
>>


In matlab I get:


>> x = method_debug_test ()

x =

  method_debug_test with no properties.

>> y = method_debug_test ()

y =

  method_debug_test with no properties.

>> [x, y]

ans =

  1×2 method_debug_test array with no properties.


Richard <crobar>

 

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

Attach Files:
   
   
Comment:
   

Attached Files
file #44160:  method_debug_test.m added by crobar (169B - text/x-objcsrc)

 

Carbon-Copy List
  • -email is unavailable- added by mmuetzel (Posted a comment)
  • -email is unavailable- added by abrac (Posted a comment)
  • -email is unavailable- added by siko1056 (Updated the item)
  • -email is unavailable- added by amro_octave (Posted a comment)
  • -email is unavailable- added by amro_octave
  • -email is unavailable- added by jwe (Updated the item)
  • -email is unavailable- added by apjanke (Posted a comment)
  • -email is unavailable- added by crobar (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 17 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2024-01-18 rik5 Dependencies- Depends on bugs #65179
    2020-12-01 mmuetzel StatusConfirmed Duplicate
        Open/ClosedOpen Closed
    2019-05-09 siko1056 Severity1 - Wish 2 - Minor
        Item GroupFeature Request Matlab Compatibility
        StatusNeed Info Confirmed
        Release4.4.0 5.1.0
        Operating SystemGNU/Linux Any
        Dependencies- Depends on bugs #44665
    2019-02-25 mtmiller Carbon-CopyRemoved 80942 -
    2019-02-25 amro_octave Carbon-Copy- Added amro_octave
    2019-01-12 jwe Severity3 - Normal 1 - Wish
        Priority5 - Normal 3 - Low
        Item GroupMatlab Compatibility Feature Request
    2018-05-15 mtmiller CategoryNone Interpreter
        StatusNone Need Info
    2018-05-15 crobar Attached File- Added method_debug_test.m, #44160

    Back to the top

    Powered by Savane 3.13-cf05.
    Corresponding source code