[bug #68108] classdef objects do not support creation of initialized arrays

Mark Goldberg <[email protected]>
Newsgroups gmane.comp.gnu.octave.bugs
Message-ID <[email protected]>
Please use the bug tracker to post updates to a bug report.  The mailing list is intended as a read-only notification stream.  Info posted to this mailing list address won't appear in the tracker database where it is most useful.

URL:
  <https://savannah.gnu.org/bugs/?68108>

                 Summary: classdef objects do not support creation of
initialized arrays
                   Group: GNU Octave
               Submitter: mag
               Submitted: Sun 01 Mar 2026 10:02:32 PM UTC
                Category: Classdef
                Severity: 3 - Normal
                Priority: 5 - Normal
              Item Group: Unexpected Error or Warning
                  Status: None
             Assigned to: None
         Originator Name: Mark Goldberg
        Originator Email:
             Open/Closed: Open
         Discussion Lock: Unlocked
                 Release: 11.1.0
        Operating System: Any
           Fixed Release: None
         Planned Release: None


    _______________________________________________________

Follow-up Comments:


-------------------------------------------------------
Date: Sun 01 Mar 2026 10:02:32 PM UTC By: Mark Goldberg <mag>
The ablity to create initialized arrays of objects is fundamental to OO design
but remains unsupported for classdef objects in GNU Octave 11.1.0. For
example, a class that operates on location data, would be reasonably expected
to form an array of location objects given an array of lat/lon
(latitude/longitude) coordinates.

Octave does support the capability with structs. One could easily define a
'Loc' struct with 'lat' and 'lon' fields and construct an initialized array of
'Loc' from an array of lat/lon data. However the same cannot be accomplished
in a classdef.

MATLAB supports the capability with both structs and classdef objects.

MRE: Given this simple value class.

classdef mre
    properties
        A
    end
    methods
        function obj = mre(a)
            if nargin > 0
                obj.A = a;
            end
        end
    end
end


The following fails with: "error: can't perform indexing operation on array of
mre objects"

m(1,3) = mre();
c = num2cell(1:3);
[m.A] = c{:};


However this works as expected:

s(1,3) = struct('A',[]);
c = num2cell(1:3);
[s.A] = c{:};


Results

octave:124> s
s =
  1x3 struct array containing the fields:
    A

octave:125> s(1)
ans =
  scalar structure containing the fields:
    A = 1

octave:126> s(2)
ans =
  scalar structure containing the fields:
    A = 2

octave:127> s(3)
ans =
  scalar structure containing the fields:
    A = 3









    _______________________________________________________

Reply to this item at:

  <https://savannah.gnu.org/bugs/?68108>

_______________________________________________
Message sent via Savannah
https://savannah.gnu.org/
signature.asc (application/pgp-signature, 228 B)
-----BEGIN PGP SIGNATURE-----

iHUEABYIAB0WIQQk97aszIMMAvLLwm6qLAuaBUf3TgUCaaS3ewAKCRCqLAuaBUf3
TvkaAQDSUv+6fGmYSb7u91S11/EjVEUSYUJwOxNDWJB/qQzeKQD/c7QblxmlPeJu
D/QgK/V4M2J941J0RwAXhWBWwwQkqQQ=
=SoDM
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.