Reputation: 353
I would like to create a file which will store properties containing desired values. Each property has to be defined as an array of struct. My current way of array of struct initialization:
classdef myClass < handle
properties(Constant)
myProp1 = struct(...
'Name', {'A','B'},...
'Value', {'1','2'});
end
end
How I wish to write my array of struct(which I feel is more clean and readable):
classdef myClass < handle
properties(Constant)
myProp1(1).Name = 'A';
myProp1(1).Value = 1;
myProp1(2).Name = 'B';
myProp1(2).Value = 2;
end
end
How should I go about achieving this?
Thanks
Upvotes: 3
Views: 1602
Reputation: 1
classdef myClass < handle
properties(Constant)
myProp1a = struct('Name','A','Value',1);
myProp1b = struct('Name','B','Value',2);
myProp1 = [myClass.myProp1a, myClass.myProp1b];
end
end
Upvotes: 0
Reputation: 684
I think it's not possible to create structures in the properties definition like you proposed. (See my comment on your question). An alternative is to create the array of structs in the constructor. Use (SetAccess=private)
, so that the properties may not be changed from outside.
% myClass.m
classdef myClass < handle
properties(SetAccess=private)
myProp1 = struct
end
methods
function obj = myClass() % constructor
obj.myProp1(1).Name = 'A';
obj.myProp1(1).Value = 1;
obj.myProp1(2).Name = 'B';
obj.myProp1(2).Value = 2;
end
end
end
Upvotes: 1
Reputation: 24127
There's nothing wrong with your original way of setting up myProp
.
But if you're concerned just about readability, you could add a private static method called something like makeMyProp
, which can be laid out as attractively as you want, which returns a filled out structure myProp
.
Then, in the properties
section, say myProp = myClass.makeMyProp;
.
Upvotes: 0
Reputation: 1105
You can solve this issue by using object composition.
It seems that the property myProp
in myClass
represents something else. For sake of simplicity I will assume it is a person (you will need to adapt the example to cover your needs). You can create a Person
class with properties Name
, Value
, ParentName
and use it in your class. The property section in myClass
would look like this:
myProp(1) = Person(name1, value1, parent_name1);
myProp(2) = Person(name2, value2, parent_name2);
...
myProp(N) = Person(nameN, valueN, parent_nameN);
Alternatively you can prepare your Person
class to accept arrays as inputs:
names = {name1, name2, ..., nameN};
values = [value1, value2, ..., valueN];
parent_names = {pname1, pname2, ..., pnameN};
... %//possibly more code here
myProp = Person(names, values, parent_names);
and the class Person
would take care of always keep them in the right order, provide setters and getters, etc.
A stub of a Person
class for the first solution would look like this (a class accepting arrays would be longer):
classdef Person < handle
properties (Access = private)
name = '';
value = 0;
parent_name = '';
end
methods (Access = public)
function this = Person(name, value, parent_name)
this.SetName(name);
this.SetValue(value);
this.SetParentName(parent_name);
end
function SetName(this, name)
this.name = name;
end
function SetValue(this, value)
this.value = value;
end
function SetParentName(this, parent_name)
this.parent_name = parent_name;
end
end
end
Upvotes: 0
Reputation: 24169
You could use enumeration
(yes, this apparently exists in MATLAB).
An example usage would be:
classdef ExampleEnum < uint8 %// Can also be another datatype
enumeration
%// name value
A (1)
B (2)
end
end
Then MATLAB automatically uses the string or the value depending on how you use your enum object (this is mentioned in the documentation).
Upvotes: -1