Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB: Array of structure initialization in CLASS properties

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

like image 890
Spooferman Avatar asked Jun 03 '26 05:06

Spooferman


1 Answers

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
like image 84
JaBe Avatar answered Jun 06 '26 05:06

JaBe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!