Thread Subject: Array of objects

Subject: Array of objects

From: Michal

Date: 14 Nov, 2008 03:20:05

Message: 1 of 6

Hi,

I want to create an array of objects. The objects are derived from the same superclass, e.g.
classdef class1
  ...
end

classdef subclass1 < class1
...
end
classdef subclass2 < class1
...
end

>> a(1) = subclass1();
>> a(2) = subclass2();
Error....

Is it possible to have array like that at all?
Thanks,
Michal

Subject: Array of objects

From: ImageAnalyst

Date: 14 Nov, 2008 03:27:30

Message: 2 of 6

On Nov 13, 10:20=A0pm, "Michal " <mj...@ksu.edu> wrote:
> Hi,
>
> I want to create an array of objects. The objects are derived from the sa=
me superclass, e.g.
> classdef class1
> =A0 ...
> end
>
> classdef subclass1 < class1
> ...
> end
> classdef subclass2 < class1
> ...
> end
>
> >> a(1) =3D subclass1();
> >> a(2) =3D subclass2();
>
> Error....
>
> Is it possible to have array like that at all?
> Thanks,
> Michal

Wouldn't "a" need to be a cell array?

Subject: Array of objects

From: Michal

Date: 14 Nov, 2008 05:51:01

Message: 3 of 6

Yes, making 'a' a cell array solves the problem!
Thanks!

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <0aacb328-2f15-40de-a6f4-7e6fd7c72f8d@u29g2000pro.googlegroups.com>...
> On Nov 13, 10:20=A0pm, "Michal " <mj...@ksu.edu> wrote:
> > Hi,
> >
> > I want to create an array of objects. The objects are derived from the sa=
> me superclass, e.g.
> > classdef class1
> > =A0 ...
> > end
> >
> > classdef subclass1 < class1
> > ...
> > end
> > classdef subclass2 < class1
> > ...
> > end
> >
> > >> a(1) =3D subclass1();
> > >> a(2) =3D subclass2();
> >
> > Error....
> >
> > Is it possible to have array like that at all?
> > Thanks,
> > Michal
>
> Wouldn't "a" need to be a cell array?

Subject: Array of objects

From: Axel P

Date: 1 Mar, 2009 08:39:01

Message: 4 of 6

There is another method, but only if class1 is not an abstract method:

a = class1; %initialize array as a class1-array
a(1) = []; %make it emtpy again, not necessary
a(1) = subclass1;
a(2) = subclass2;

My big question is now, how do I do this if class1 is an abstract class?
The reason I don't want to use cell arrays is that it is programmatically not nice and anything could be stored in that array, whereas in the other method, just class1 based objects are allowed.

Thanks,
Axel


"Michal " <mjama@ksu.edu> wrote in message <gfj3k5$jcs$1@fred.mathworks.com>...
> Yes, making 'a' a cell array solves the problem!
> Thanks!
>
> ImageAnalyst <imageanalyst@mailinator.com> wrote in message <0aacb328-2f15-40de-a6f4-7e6fd7c72f8d@u29g2000pro.googlegroups.com>...
> > On Nov 13, 10:20=A0pm, "Michal " <mj...@ksu.edu> wrote:
> > > Hi,
> > >
> > > I want to create an array of objects. The objects are derived from the sa=
> > me superclass, e.g.
> > > classdef class1
> > > =A0 ...
> > > end
> > >
> > > classdef subclass1 < class1
> > > ...
> > > end
> > > classdef subclass2 < class1
> > > ...
> > > end
> > >
> > > >> a(1) =3D subclass1();
> > > >> a(2) =3D subclass2();
> > >
> > > Error....
> > >
> > > Is it possible to have array like that at all?
> > > Thanks,
> > > Michal
> >
> > Wouldn't "a" need to be a cell array?

Subject: Array of objects

From: Matt

Date: 1 Mar, 2009 15:55:03

Message: 5 of 6

"Axel P" <xboarder@gmx.net> wrote in message <godhj5$4q3$1@fred.mathworks.com>...
> There is another method, but only if class1 is not an abstract method:
>
> a = class1; %initialize array as a class1-array
> a(1) = []; %make it emtpy again, not necessary
> a(1) = subclass1;
> a(2) = subclass2;
>
> My big question is now, how do I do this if class1 is an abstract class?
> The reason I don't want to use cell arrays is that it is programmatically not nice and anything could be stored in that array, whereas in the other method, just class1 based objects are allowed.

I don't believe you can do that. An abstract class cannot be instantiated in an array or otherwise.

Also, I don't believe what you showed above accomplished what the OP intends. If your code above did not give you an error, it is only because subclass1 and subclass2 possess converter methods which turn them into instances of class1. They do not exist in the final array 'a' as subclass objects.

Subject: Array of objects

From: Axel P

Date: 2 Mar, 2009 03:23:01

Message: 6 of 6

Thanks Matt, you are right. That seems to work at first sight (no error msg), but the objects are all of class_1. So you probably have to use a cell array.


> I don't believe you can do that. An abstract class cannot be instantiated in an array or otherwise.
>
> Also, I don't believe what you showed above accomplished what the OP intends. If your code above did not give you an error, it is only because subclass1 and subclass2 possess converter methods which turn them into instances of class1. They do not exist in the final array 'a' as subclass objects.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
oop Axel P 1 Mar, 2009 03:40:08
abstract objects Axel P 1 Mar, 2009 03:40:08
array of abstra... Axel P 1 Mar, 2009 03:40:08
objectoriented ... Michal 13 Nov, 2008 22:25:05
array of objects Michal 13 Nov, 2008 22:25:05
rssFeed for this Thread

Contact us at files@mathworks.com