Class Property: Array of Objects (2024)

37 views (last 30 days)

Show older comments

Nycholas Maia on 28 Apr 2017

  • Link

    Direct link to this question

    https://www.mathworks.ca/matlabcentral/answers/337734-class-property-array-of-objects

  • Link

    Direct link to this question

    https://www.mathworks.ca/matlabcentral/answers/337734-class-property-array-of-objects

Answered: Nycholas Maia on 29 Apr 2017

Accepted Answer: Guillaume

I would like to create a "Dependent Property" inside a "Main Class" that will handle a array of objects of "Sub Class".

Exemple:

classdef main_class

properties

number

end

properties (Dependent)

% prop will be a array of sub_class object

% the size of this array will depend/change on the "number" property

prop = sub_class();

end

end

If I set:

main = main_class();

main.number = 3;

I would like to get:

main.prop = [sub_class(1) sub_class(2) sub_class(3)]

To me is important that "prop" be a dependent property to automatic change the number of the elements in the array.

How could I do that? Thanks!

1 Comment

Show -1 older commentsHide -1 older comments

Adam on 28 Apr 2017

Direct link to this comment

https://www.mathworks.ca/matlabcentral/answers/337734-class-property-array-of-objects#comment_449616

  • Link

    Direct link to this comment

    https://www.mathworks.ca/matlabcentral/answers/337734-class-property-array-of-objects#comment_449616

Edited: Adam on 28 Apr 2017

I would strongly recommend reconsidering your wish to have this as a dependent property. I use dependent properties a lot and I like them, but in this case they have some serious difficulties.

Creating objects in a dependent property is highly unlikely to be what you want to do. This means that every time you access the dependent property it will create a new array of objects, you will never be able to access the same objects within the class (though you can, of course, if you store main.prop in a local variable and use it thereafter) because they will keep getting replaced every time you get the property 'prop'.

Even if this does happen to produce the behaviour you want it is not very transparent and I would always do something as fundamental as object creation in a function named e.g. createXXXX to be clear what it is doing, not as a side-effect of accessing a dependent property.

Also it is not clear - is sub_class actually a sub class of main_class? As in does it inherit from it, or do you just call it sub_class because it sits inside it? Objects containing objects of their own subclass feels like an odd design to me that would cause a lot of potential problems. Maybe I'm just not thinking of a usage though...

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Guillaume on 28 Apr 2017

  • Link

    Direct link to this answer

    https://www.mathworks.ca/matlabcentral/answers/337734-class-property-array-of-objects#answer_264886

  • Link

    Direct link to this answer

    https://www.mathworks.ca/matlabcentral/answers/337734-class-property-array-of-objects#answer_264886

Edited: Guillaume on 28 Apr 2017

I'm with Adam, I don't think you want a dependent property. dependent does not mean that the property value is recalculated when some other property change, it means that property value is created every time you ask for it. That value is not stored in the class.

In your case, you would be better off creating a setter method for your number property that automatically update the array size of the normal prop property:

classdef main_class

properties

number

prop

end

methods

function this = main_class(number) %constructor

if nargin == 0

this.number = 0;

else

this.number = number;

end

this = this.create_subarray;

end

function this = set.number(this, value) %set method for number. reset prop at the same time

this.number = value;

this = this.create_subarray;

end

function this = create_subarray(this)

this.prop = sub_class.empty;

for n = 1:this.number

this.prop(n) = sub_class(n);

end

end

end

end

As Adam and Andrew have commented, hopefully sub_class is not actually a subclass of main class.

6 Comments

Show 4 older commentsHide 4 older comments

Nycholas Maia on 28 Apr 2017

Direct link to this comment

https://www.mathworks.ca/matlabcentral/answers/337734-class-property-array-of-objects#comment_449647

  • Link

    Direct link to this comment

    https://www.mathworks.ca/matlabcentral/answers/337734-class-property-array-of-objects#comment_449647

Edited: Nycholas Maia on 28 Apr 2017

Yes! You all are right!

In my case, I will not use a "dependent property" because I want to access the values stored in the memory.

I think that I'm understanding...

Just to be clear:

Question A: What are: "n" and "value" variables in the Guilleaume code?

Question B: I want add some "properties" inside the prop variable. Like a "struct" or "object" inside the prop propertie.

Example:

Main = main_class(10);

Main.prop(5).color = 'blue';

Main.prop(5).size = 10;

Main.prop(5).collection = [struct struct struct];

Please, how can I do that?

Is there a way to define the properties inside the prop? Like a class, I could define the name and type of all properties inside prop. Is this possible?

Thank you so much!

Adam on 28 Apr 2017

Direct link to this comment

https://www.mathworks.ca/matlabcentral/answers/337734-class-property-array-of-objects#comment_449653

  • Link

    Direct link to this comment

    https://www.mathworks.ca/matlabcentral/answers/337734-class-property-array-of-objects#comment_449653

I think the 1:n in Guillaume's code is a typo and should be 1:number instead. The other n's are just place-holder variables in an arrayfun expression, for each of the 1:number values in succession.

'prop' will be an array of classes so yes you can set properties on them. If you are wanting to make lots of changes then having them embedded inside another class is not necessarily the best option, but it depends on the situation really.

Nycholas Maia on 28 Apr 2017

Direct link to this comment

https://www.mathworks.ca/matlabcentral/answers/337734-class-property-array-of-objects#comment_449661

  • Link

    Direct link to this comment

    https://www.mathworks.ca/matlabcentral/answers/337734-class-property-array-of-objects#comment_449661

Adam, you said that "prop will be an array of classes"...

So, I need to create a new M file and create a new class?

Could you please write a little example?

Adam on 28 Apr 2017

Direct link to this comment

https://www.mathworks.ca/matlabcentral/answers/337734-class-property-array-of-objects#comment_449664

  • Link

    Direct link to this comment

    https://www.mathworks.ca/matlabcentral/answers/337734-class-property-array-of-objects#comment_449664

Well, my comment was a little inaccurate - it will be an array of class objects to be more accurate.

You told us in the original question though that you already have this class, it is called 'sub_class' so prop will be an array of objects of this type, whatever is in that class.

Nycholas Maia on 28 Apr 2017

Direct link to this comment

https://www.mathworks.ca/matlabcentral/answers/337734-class-property-array-of-objects#comment_449668

  • Link

    Direct link to this comment

    https://www.mathworks.ca/matlabcentral/answers/337734-class-property-array-of-objects#comment_449668

Thank you Adam for your quick response!

I will try this solution and I will give a feedback as soon as possible!

Thank you all!

Guillaume on 28 Apr 2017

Direct link to this comment

https://www.mathworks.ca/matlabcentral/answers/337734-class-property-array-of-objects#comment_449680

  • Link

    Direct link to this comment

    https://www.mathworks.ca/matlabcentral/answers/337734-class-property-array-of-objects#comment_449680

Yes, as Adam pointed out there were a few typos in my example (now corrected). In addition, I'd forgotten that arrayfun cannot create arrays of object, so I've changed the implementation to an explicit loop.

prop can be anything you want, array of class objects (whose class you define in a separate m file, a structure array (in which case you don't need another file), or something else.

Note that I've ignored methods and properties attributes in my example. I assumed that prop would be read-only. If prop is read/write as your

Main.prop(5).color = 'blue';

would require (assumin that sub_class is a value class), then the user can also change the size of the prop array independently of the number property, breaking your class. If you want to avoid that, you'd also need a property setter for prop, e.g:

function this = set.prop(this, value);

assert(isequal(size(value), size(this.prop)), 'Resizing prop is not allowed');

this.prop = value;

end

Sign in to comment.

More Answers (2)

Andrew Newell on 28 Apr 2017

  • Link

    Direct link to this answer

    https://www.mathworks.ca/matlabcentral/answers/337734-class-property-array-of-objects#answer_264855

  • Link

    Direct link to this answer

    https://www.mathworks.ca/matlabcentral/answers/337734-class-property-array-of-objects#answer_264855

You need to define a get method (see Access Methods for Dependent Properties). For example, the method block could look like this:

methods

function value = get.prop(obj)

value(obj.number) = sub_class(obj.number);

for ii=1:obj.number-1

value(ii) = sub_class(ii);

end

end

end

Note that, since sub_class would share this method, the dependent variable is endlessly recursive; if you ran

main = main_class();

main.number = 1;

then main.prop = main.prop.prop = ...

2 Comments

Show NoneHide None

Nycholas Maia on 28 Apr 2017

Direct link to this comment

https://www.mathworks.ca/matlabcentral/answers/337734-class-property-array-of-objects#comment_449611

  • Link

    Direct link to this comment

    https://www.mathworks.ca/matlabcentral/answers/337734-class-property-array-of-objects#comment_449611

Edited: Nycholas Maia on 28 Apr 2017

Hi Andrew, thanks for your response.

But I'm newer in MATLAB OOP, and I did not understand how your code will return a array of objects of "sub_class()" inside the "dependent property" prop.

Could you explain better for a young MATLAB programmer? Obs.: My doubt is not the get/set idea. Im a C++ programmer.

Thank you!

Andrew Newell on 28 Apr 2017

Direct link to this comment

https://www.mathworks.ca/matlabcentral/answers/337734-class-property-array-of-objects#comment_449707

  • Link

    Direct link to this comment

    https://www.mathworks.ca/matlabcentral/answers/337734-class-property-array-of-objects#comment_449707

If sub_class is actually a subclass of main_class, it inherits the properties and methods of main_class. That would include the property number and the method get.prop. So if you type

main.prop.prop

get.prop will create a sub_class object and return it as main.prop. Then, in main.prop, get.prop will create yet another sub_class object and return it as main.prop.prop. I agree with Adam and Guillaume - this is not the way to go!

Sign in to comment.

Nycholas Maia on 29 Apr 2017

  • Link

    Direct link to this answer

    https://www.mathworks.ca/matlabcentral/answers/337734-class-property-array-of-objects#answer_265045

  • Link

    Direct link to this answer

    https://www.mathworks.ca/matlabcentral/answers/337734-class-property-array-of-objects#answer_265045

Thank you all....now I got everything working great! Thank you so much!

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABProgrammingClassesConstruct and Work with Object Arrays

Find more on Construct and Work with Object Arrays in Help Center and File Exchange

Tags

  • array
  • object
  • class
  • properties
  • dependent
  • oop

Products

  • MATLAB

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Class Property: Array of Objects (14)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

Europe

Asia Pacific

Contact your local office

Class Property: Array of Objects (2024)

References

Top Articles
Latest Posts
Article information

Author: Arline Emard IV

Last Updated:

Views: 6628

Rating: 4.1 / 5 (52 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Arline Emard IV

Birthday: 1996-07-10

Address: 8912 Hintz Shore, West Louie, AZ 69363-0747

Phone: +13454700762376

Job: Administration Technician

Hobby: Paintball, Horseback riding, Cycling, Running, Macrame, Playing musical instruments, Soapmaking

Introduction: My name is Arline Emard IV, I am a cheerful, gorgeous, colorful, joyous, excited, super, inquisitive person who loves writing and wants to share my knowledge and understanding with you.