MATLAB: structure initialization array in CLASS properties

I would like to create a file that will store properties containing the values ​​I want. Each property must be defined as an array of the structure. My current way of arranging a struct is:

classdef myClass < handle

    properties(Constant)
          myProp1  = struct(...
                     'Name', {'A','B'},...
                     'Value', {'1','2'});
    end

end

      

How I want to write my structure array (which I find cleaner and more 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 can I achieve this?

thank

+3


source to share


4 answers


I think it is not possible to create structs in property definitions as you suggested. (See my comment on your question). An alternative is to create an array of structures in the constructor. Use (SetAccess=private)

to keep properties from changing 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

      

+1


source


You can solve this problem using object structure .

It seems that the property myProp

in myClass

represents something else. For the sake of simplicity, I will assume this is a human (you will need to adapt the example to suit your needs). You can create a class Person

with properties Name

, Value

, ParentName

and use it in class. The properties section myClass

will 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 class Person

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

will take care of keeping them in the correct order at all times, providing setters and getters, etc.

The class closure Person

for the first solution will look like this (the class accepting arrays will 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

      

0


source


There is nothing wrong with your initial setup myProp

.

But if you're only interested in readability, you can add a private static method called something like makeMyProp

that, which can be laid out as attractively as you want, that returns a filled structure myProp

.

Then in the section properties

let's say myProp = myClass.makeMyProp;

.

0


source


You can use enumeration

(yes it seems to exist in MATLAB).

Usage example:

classdef ExampleEnum < uint8 %// Can also be another datatype
    enumeration
    %//  name  value
          A     (1)
          B     (2)
    end    
end

      

MATLAB then automatically uses the string or value depending on how you use your enum object (this is stated in the documentation).

-1


source







All Articles