Thread Subject: Matrix of subclasses with same base class, how? Concatenate fails

Subject: Matrix of subclasses with same base class, how? Concatenate fails

From: Greg

Date: 30 May, 2008 21:10:04

Message: 1 of 3

Hi,

I have a class hierarchy (descends from handle)

MyBase < Handle

MySubOne < MyBase

MySubTwo < MyBase

MySubThree < MySubOne

etc.

I want to be able to put instances of these in a list and
iterate over them. However when I try to add a second
instance of a subclass to a matrix holding another subclass
I get error:

The following error occurred converting from MySubTwo to
MySubOne:
Undefined function or method 'isfinite' for input arguments
of type 'MySubTwo'.


Error in ==> deleted...
           obj.listOfSubClasses = [obj.listOfSubClasses;
instanceOfSubClass]; %add it to the list

I first add an instance of MySubOne then get the error when
I try to add MySubTwo.

Any ideas?

Subject: Matrix of subclasses with same base class, how? Concatenate fails

From: Greg

Date: 31 May, 2008 20:05:04

Message: 2 of 3

Well the work around is to use a cell array instead.
Although I feel that two classes sharing the same base class
should be able to be put in a matrix...

anyway here is what I did:

Code to add class objects to list:

if (size(listOfSubClasses,2) > 0)
    tempCell = {;instOfSubClassToAddToList};
    listOfSubClasses = [listOfSubClasses tempCell];
else
%otherwise we end up with empty cell at beginning of list
    listOfSubClasses = {;instOfSubClassToAddToList};
       
end

This will give you a 1xn array.

Now to reference one of the objects in the list:

listOfSubClasses{cnt}.methodOfYourClass()

where cnt is from 1 to the size of the list (use either
size(list,2) or length(list) to get the size.




"Greg " <greg9504@removeforspam.hotmail.com> wrote in
message <g1pqfc$kdd$1@fred.mathworks.com>...
> Hi,
>
> I have a class hierarchy (descends from handle)
>
> MyBase < Handle
>
> MySubOne < MyBase
>
> MySubTwo < MyBase
>
> MySubThree < MySubOne
>

Subject: Matrix of subclasses with same base class, how? Concatenate fails

From: Steven Lord

Date: 2 Jun, 2008 02:45:19

Message: 3 of 3


"Greg " <greg9504@removeforspam.hotmail.com> wrote in message
news:g1sb1g$9tu$1@fred.mathworks.com...
> Well the work around is to use a cell array instead.

Yes.

> Although I feel that two classes sharing the same base class
> should be able to be put in a matrix...

Let's say that A is an object that is both an animal and a dog, and B is an
object that is both an animal and a cat. Both the dog class and the cat
class have additional properties and/or methods that aren't in the animal
class.

What class should C = [A; B] be?

It can't be a dog array, unless you can convert the cat animal B into a dog
(which usually doesn't work too well.)
It can't be a cat array, since you can't convert A into a cat.
It can't be an animal array either, since each of A and B have pieces that
aren't in the animal class that would have to be removed to fit them into an
animal array.

It doesn't make sense for C to be an animal array, a dog array, or a cat
array. Therefore, IMHO it doesn't really make sense to allow concatenating
A and B.

--
Steve Lord
slord@mathworks.com

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
concatenate cla... Greg 30 May, 2008 17:10:06
matrix Greg 30 May, 2008 17:10:05
subclass Greg 30 May, 2008 17:10:05
classes Greg 30 May, 2008 17:10:05
rssFeed for this Thread

Contact us at files@mathworks.com