Got Questions? Get Answers.
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:
How to initialize an object array?

Subject: How to initialize an object array?

From: Yuheng

Date: 6 Jul, 2012 03:09:08

Message: 1 of 5

Hi,
First time I use a class in matlab, I have met a problem in the initialization of object arrays.
Is there any function like ‘zeros’ or ‘repmat’ to initialize any user defined class, or need I to write a function for each class I used separately?
I find the function ‘class’ can be used to convert a structure array to object array, but it seems not available in a classdef block.

Any help would be appreciated.
Thanks!

Subject: How to initialize an object array?

From: vedenev.maxim@gmail.com

Date: 6 Jul, 2012 06:34:30

Message: 2 of 5

Hi
If you made a class say my_class then in matlab you crate a instance:
my_class1=my_class(...parameters...)
To create many instancies use for loop and cell array
my_instancies=cell(10,1);
for c=1:10
    my_instancies{c}=my_class(...parameters...)
end

------------------------------------
Maxim Vedenev, Matlab freelancer
vedenev@ngs.ru
http://simulations.narod.ru/

Subject: How to initialize an object array?

From: Steven_Lord

Date: 6 Jul, 2012 13:38:58

Message: 3 of 5



"Yuheng " <e2_71828@tom.com> wrote in message
news:jt5ksk$ck7$1@newscl01ah.mathworks.com...
> Hi,
> First time I use a class in matlab, I have met a problem in the
> initialization of object arrays.
> Is there any function like zeros or repmat to initialize any user
> defined class, or need I to write a function for each class I used
> separately?
> I find the function class can be used to convert a structure array to
> object array, but it seems not available in a classdef block.

Are you using a class that someone else wrote, or are you writing your own
class?

If the latter, have you read through this section of the documentation? It
describes how to handle creation and manipulation of object arrays in your
class.

http://www.mathworks.com/help/techdoc/matlab_oop/brd4btr.html

If the former, it depends on how the class was implemented. REPMAT should
work to replicate a scalar object into an array (unless the author of the
class specifically disallowed that.)

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: How to initialize an object array?

From: Yuheng

Date: 8 Jul, 2012 08:30:08

Message: 4 of 5

vedenev.maxim@gmail.com wrote in message <ef3bfd6f-caa6-42d4-9cec-59d6330dc9cb@googlegroups.com>...
> Hi
> If you made a class say my_class then in matlab you crate a instance:
> my_class1=my_class(...parameters...)
> To create many instancies use for loop and cell array
> my_instancies=cell(10,1);
> for c=1:10
> my_instancies{c}=my_class(...parameters...)
> end
>
> ------------------------------------
> Maxim Vedenev, Matlab freelancer
> vedenev@ngs.ru
> http://simulations.narod.ru/

That works, thank you!

Subject: How to initialize an object array?

From: Yuheng

Date: 8 Jul, 2012 08:43:07

Message: 5 of 5

"Steven_Lord" <slord@mathworks.com> wrote in message <jt6ppi$il6$1@newscl01ah.mathworks.com>...
>
>
> "Yuheng " <e2_71828@tom.com> wrote in message
> news:jt5ksk$ck7$1@newscl01ah.mathworks.com...
> > Hi,
> > First time I use a class in matlab, I have met a problem in the
> > initialization of object arrays.
> > Is there any function like zeros or repmat to initialize any user
> > defined class, or need I to write a function for each class I used
> > separately?
> > I find the function class can be used to convert a structure array to
> > object array, but it seems not available in a classdef block.
>
> Are you using a class that someone else wrote, or are you writing your own
> class?
>
> If the latter, have you read through this section of the documentation? It
> describes how to handle creation and manipulation of object arrays in your
> class.
>
> http://www.mathworks.com/help/techdoc/matlab_oop/brd4btr.html
>
> If the former, it depends on how the class was implemented. REPMAT should
> work to replicate a scalar object into an array (unless the author of the
> class specifically disallowed that.)
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

I am writing my own class, and the link is really helpful. I did not take the case nargin=0 into account, after modified that, repmat works.

Thanks a lot!

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