Home > Net >  How to set the size validator of a class property to a known constant
How to set the size validator of a class property to a known constant

Time:12-10

I would like to do something like the following code where all 3 properties is statically known to be size (1,10) without having to explicitly re-write the 10 at the property declaration.

classdef Example
    properties(Constant)
        length_of_vector = 10;
    end
    properties
        x_data(1, Example.length_of_vector);
        y_data(1, Example.length_of_vector);
        z_data(1, Example.length_of_vector);
    end
end

This syntax is not valid, is there a way to accomplish this without re-writing 10 in all three places? My real use-case has several dimensions that have statically known sizes and I'd really like to be able to specify what length they are in the declaration so that maintainers will know what size is expected but the constants can be changed and it automatically updates all the property sizes that depend on it.

CodePudding user response:

Example.length_of_vector is valid inside methods of the class, and outside the class as well. I guess it is not valid in your code because MATLAB is still loading the class definition when it encounters Example.length_of_vector, but Example is not yet loaded.

I can think of two ways to work around this:

  1. Declare the size of the properties in the constructor:

    function obj = Example
       obj.x_data = zeros(1, Example.length_of_vector);
       %...
    end
    
  2. Define your constants in a different way. A common method is to use a function. You can put this function at the end of your classdef file, outside the classdef block:

    classdef Example
        properties
            x_data(1, length_of_vector);
            y_data(1, length_of_vector);
            z_data(1, length_of_vector);
        end
    end
    
    function l = length_of_vector
       l = 10;
    end
    

    With this method, your constant is private to your class, it cannot be accessed from outside. To make it public, you would have to add a static method to your class that returns the constant.

CodePudding user response:

hope the code help:

classdef Example  
        properties 
            length_of_vector = 10;
            x_data ;
            y_data ;
            z_data ;
        end
        methods 
             % if u wont change the properties use function obj = Example()
             % if u want to change the properties use function obj = New_value(obj)
             function obj = New_value(obj)
             obj.x_data = zeros(1, obj.length_of_vector);
             obj.y_data = zeros(1, obj.length_of_vector);
             obj.z_data = zeros(1, obj.length_of_vector);
          end
        end
    end

useCase :

a = example;

outPut :

a = 
   length_of_vector: 10
              x_data: []
              y_data: []
              z_data: []

then :

a.New_value;

outPut :

a = 
     length_of_vector: 10
              x_data: [0 0 0 0 0 0 0 0 0 0]
              y_data: [0 0 0 0 0 0 0 0 0 0]
              z_data: [0 0 0 0 0 0 0 0 0 0]

when you want to change the length:

a.length_of_vector = 15;
a.New_value

outPut :

a = 
      length_of_vector: 15
              x_data: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
              y_data: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
              z_data: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  • Related