Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
defining class similar to cell array

Subject: defining class similar to cell array

From: Amit

Date: 30 Jun, 2010 20:59:04

Message: 1 of 4

Hi,
I want to define a class that is needed to store paths of a road network and their attributes.
path is the sequence of nodes (sequence of uint16) of the network. attributes of paths are pathflow (double) and pathcost (double).

I created a class like this-
classdef amitdata
    properties
        path
        pathflo
        pathcost
    end
end

Now I want to create objects of this class. There are two paths for going from node 1 to node 3 as: 1-2-3 and 1-2-4-3. Hence I want to a object like this
a(1).path=uint16([1,2,3]);
a(1).path(2)=uint16([1,2,4,3]);

But I get the error
"??? In an assignment A(:) = B, the number of elements in A and B
must be the same."
This means I can not have elements of this field of different lengths. I don't want to use cell array as it has overhead of 60 bytes for each cell. I have to store a state wide road network and using cell will leave me out of memory.

Please help me to overcome this problem.

Thanks,
Amit

Subject: defining class similar to cell array

From: Matt J

Date: 1 Jul, 2010 00:00:22

Message: 2 of 4

"Amit " <amitsingh.singh@gmail.com> wrote in message <i0gb6o$lke$1@fred.mathworks.com>...

> Now I want to create objects of this class. There are two paths for going from node 1 to node 3 as: 1-2-3 and 1-2-4-3. Hence I want to a object like this
> a(1).path=uint16([1,2,3]);
> a(1).path(2)=uint16([1,2,4,3]);
>
> But I get the error
> "??? In an assignment A(:) = B, the number of elements in A and B
> must be the same."
> This means I can not have elements of this field of different lengths. I don't want to use cell array as it has overhead of 60 bytes for each cell. I have to store a state wide road network and using cell will leave me out of memory.
==================

You could concatenate the paths into a single vector and store indices to mark the beginning of each path fragment, e.g.

a(1).path=uint16([1 2 3 1 2 4 3]);
a(1).indices=[1 4];

Subject: defining class similar to cell array

From: Amit

Date: 1 Jul, 2010 20:00:21

Message: 3 of 4

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <i0glqm$6qg$1@fred.mathworks.com>...
> "Amit " <amitsingh.singh@gmail.com> wrote in message <i0gb6o$lke$1@fred.mathworks.com>...
>
> > Now I want to create objects of this class. There are two paths for going from node 1 to node 3 as: 1-2-3 and 1-2-4-3. Hence I want to a object like this
> > a(1).path=uint16([1,2,3]);
> > a(1).path(2)=uint16([1,2,4,3]);
> >
> > But I get the error
> > "??? In an assignment A(:) = B, the number of elements in A and B
> > must be the same."
> > This means I can not have elements of this field of different lengths. I don't want to use cell array as it has overhead of 60 bytes for each cell. I have to store a state wide road network and using cell will leave me out of memory.
> ==================
>
> You could concatenate the paths into a single vector and store indices to mark the beginning of each path fragment, e.g.
>
> a(1).path=uint16([1 2 3 1 2 4 3]);
> a(1).indices=[1 4];

Thanks Matt, for your help. I have one more question, if I have may paths (in the following case 3) how can I get them as node sequence without using loop. I know the cat command will concatenate but how to retrieve the elements back or in particular how to get second path.
if paths are-
 p1=[1 2 3] ; p2=[1 2 4 3] ; p3=[1 2 6 7 3] ;
% path1:1 2 3, Path2: 1 2 4 3, path3:1 2 6 7 3

then to store path I can write-
a(1).indices=[1, length(p1)+1, length(p1)+length(p2)+1] ; %saving indices
a(1).path=uint16(cat(2,p1,p2,p3));
clear p1 p2 p3 % delete paths to save space

Now, if later I want retrieve the paths (1,2,3) as three vectors and second if I just need the path2 then how to get that.
Thanks in advance,
-Amit

Subject: defining class similar to cell array

From: Matt J

Date: 1 Jul, 2010 20:25:19

Message: 4 of 4

"Amit " <amitsingh.singh@gmail.com> wrote in message <i0is4l$28r$1@fred.mathworks.com>...
> "Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <i0glqm$6qg$1@fred.mathworks.com>...
> > "Amit " <amitsingh.singh@gmail.com> wrote in message <i0gb6o$lke$1@fred.mathworks.com>...
> >
> > > Now I want to create objects of this class. There are two paths for going from node 1 to node 3 as: 1-2-3 and 1-2-4-3. Hence I want to a object like this
> > > a(1).path=uint16([1,2,3]);
> > > a(1).path(2)=uint16([1,2,4,3]);
> > >
> > > But I get the error
> > > "??? In an assignment A(:) = B, the number of elements in A and B
> > > must be the same."
> > > This means I can not have elements of this field of different lengths. I don't want to use cell array as it has overhead of 60 bytes for each cell. I have to store a state wide road network and using cell will leave me out of memory.
> > ==================
> >
> > You could concatenate the paths into a single vector and store indices to mark the beginning of each path fragment, e.g.
> >
> > a(1).path=uint16([1 2 3 1 2 4 3]);
> > a(1).indices=[1 4];
>
> Thanks Matt, for your help. I have one more question, if I have may paths (in the following case 3) how can I get them as node sequence without using loop. I know the cat command will concatenate but how to retrieve the elements back or in particular how to get second path.
> if paths are-
> p1=[1 2 3] ; p2=[1 2 4 3] ; p3=[1 2 6 7 3] ;
> % path1:1 2 3, Path2: 1 2 4 3, path3:1 2 6 7 3
>
> then to store path I can write-
> a(1).indices=[1, length(p1)+1, length(p1)+length(p2)+1] ; %saving indices
> a(1).path=uint16(cat(2,p1,p2,p3));
> clear p1 p2 p3 % delete paths to save space
>
> Now, if later I want retrieve the paths (1,2,3) as three vectors and second if I just need the path2 then how to get that.
> Thanks in advance,
=====

You could write a getpaths class method

function [varargout]=getpaths(obj,pathnum)

 Lengths=diff( [obj.indices, length(obj.path)+1] );

 for ii=1:nargout

   idx=obj.indices( pathnum(ii) );

   varargout{ii}=obj.path(idx:idx+Lengths(ii)-1);

 end

end

Tags for this Thread

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.

Contact us