Thread Subject: Array of class objects?

Subject: Array of class objects?

From: David Doria

Date: 13 Oct, 2009 18:50:18

Message: 1 of 4

If I have a class:

classdef DataPoint
    
    properties
        Point
        ClassId
    end
    
    methods
        function DP = DataPoint(Point, ClassId)
            DP.Point = Point;
            DP.ClassId = ClassId;
        end
        function display(DP)
            disp(['DataPoint: x=', num2str(DP.Point(1)), ' y=', num2str(DP.Point(2))])
        end

    end
    
end

And I want a bunch of DataPoint's in an array, I thought I would make a cell array and store them there:

mypoint = DataPoint([0 2], 1)
test = cell(5,1);
test(1) = mypoint


However I get this error:
??? The following error occurred converting from DataPoint to cell:
Error using ==> cell
Conversion to cell from DataPoint is not possible.
 
How should I make an array of DataPoint's?

Thanks,

Dave

Subject: Array of class objects?

From: Rich Ellis

Date: 13 Oct, 2009 21:09:24

Message: 2 of 4

I think you want this syntax:

test(1) = {mypoint}


"David Doria" <daviddoria@gmail.com> wrote in message
news:hb2i5a$hp4$1@fred.mathworks.com...
> If I have a class:
>
> classdef DataPoint
>
> properties
> Point
> ClassId
> end
>
> methods
> function DP = DataPoint(Point, ClassId)
> DP.Point = Point;
> DP.ClassId = ClassId;
> end
> function display(DP)
> disp(['DataPoint: x=', num2str(DP.Point(1)), ' y=',
> num2str(DP.Point(2))])
> end
>
> end
>
> end
>
> And I want a bunch of DataPoint's in an array, I thought I would make a
> cell array and store them there:
>
> mypoint = DataPoint([0 2], 1)
> test = cell(5,1);
> test(1) = mypoint
>
>
> However I get this error:
> ??? The following error occurred converting from DataPoint to cell:
> Error using ==> cell
> Conversion to cell from DataPoint is not possible.
>
> How should I make an array of DataPoint's?
>
> Thanks,
>
> Dave
>

Subject: Array of class objects?

From: David Doria

Date: 19 Oct, 2009 12:53:18

Message: 3 of 4

"Rich Ellis" <rich@mathworks.com> wrote in message <hb2qa8$f9m$1@fred.mathworks.com>...
> I think you want this syntax:
>
> test(1) = {mypoint}
>

Yep, that's the one - thanks!

Subject: Array of class objects?

From: Peter Perkins

Date: 19 Oct, 2009 17:38:15

Message: 4 of 4

David Doria wrote:
> If I have a class:
>
> classdef DataPoint
[snip]

> And I want a bunch of DataPoint's in an array, I thought I would make a cell array and store them there:

Why don't you just concatenate them into a DataPoint array?

Tags for this Thread

Everyone's Tags:

oop

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 per isakson 19 Oct, 2009 09:25:23
rssFeed for this Thread

Contact us at files@mathworks.com