Info

This question is closed. Reopen it to edit or answer.

How to create OOP properties with subfileds

1 view (last 30 days)
Armindo
Armindo on 26 Jan 2016
Closed: MATLAB Answer Bot on 20 Aug 2021
Hi Anyone can explain me how to create an object like this (like a struct with fields):
properties
Name
Tasks
end
But inside the property task I should have properties like:
Tasks.Name
Tasks.Size
for which I can validateattributes and use the get and set methods (matlab.mixin.SetGet)

Answers (2)

Adam
Adam on 26 Jan 2016
Usually I do this by having Tasks also be an object of a class so that class will have the required properties.
You can do it with a struct though just as you would outside of a class. You cannot define the fields as such in the properties block (though I guess you can initialise to an empty struct with specified fields - I don't use structs so I haven't really tried this).
The fields of Tasks would then get created wherever you have the necessary data to put in them.
I think you would need to give more information what you are trying to do. I've never used matlab.mixin.SetGet as I tend to just use ordinary get and set functions that I write with a validateattributes at the start of all my set functions, but I don't see why it would be incompatible with what you are asking.
validating a struct for lots of fields is a pain though in general and is one of the reasons I almost exclusively use classes even if my class is effectively just a data structure with noting but public get and set functions that validate what is set. It means that in the example you give you don't need to validate struct fields because 'Tasks' would be an object of a class where the properties are clearly defined already.

Guillaume
Guillaume on 26 Jan 2016
As Adam says, once you've gone OOP you may as well go OOP all the way and have Task an instance of another class. Otherwise, you can simply initialise your property as a structure:
classdef ClassName
properties
Name;
Task = struct('Name', [], 'Size', []);
end
You should also define a setter for the Task property otherwise it can just be replaced by anything after construction
methods (Access = public)
function this = set.Task(this, value)
assert(isstruct(value) && all(ismember(fieldnames(value), {'Name', 'Size'})));
this.Task = value;
end
end
end
  2 Comments
Armindo
Armindo on 27 Jan 2016
Thank you very much to all for the help. As I suspected I should use 2 objects. Initialize the struct is also good but then I get and error saing that the class is mixing handle and values...
Guillaume
Guillaume on 27 Jan 2016
The warning about mixing handle and value class has nothing to do with using structures.
You do have to change the signature of the setter if the class is a handle class, though:
methods (Access = public)
function set.Task(this, value) %setter signature for a handle class
assert(isstruct(value) && all(ismember(fieldnames(value), {'Name', 'Size'})));
this.Task = value;
end
end
end

This question is closed.

Community Treasure Hunt

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

Start Hunting!