Search code examples
matlabmatlab-class

Dependent properties of Matlab class that can store values using setters


Matlab class properties have the following two limitations which are relevant to my problem,

  1. Dependent properties cannot store values
  2. setter for Properties (normal properties with no specified attributes, access specifiers, etc) cannot access other properties.

In my scenario, I need a workaround which will allow me to have a dependent property which can also store value. The dependence on another property is only for a conditional statement and not for assigning it's value with the other property itself. This scenario is illustrated by the code snippet given below, which is also my requirement that Matlab does not allow.

classdef foo
properties
    a
    b
end
properties (Dependent = true)
    c
end
methods
    function this = foo(c_init)
        a = 1;
        b = 1;
        this.c = c_init;
    end
    function this = set.c(this,value)
        if b==1
            c = value;
        else
            c = 1;
        end
    end
    function value = get.c(this)
        value = this.c;
    end
end
end

Is there any workaround for the above?


Solution

  • First of all, you can definitely have a property set function access the value of another property, it's just not completely recommended since the validity of that other property is unknown particularly during object creation. This will issue an mlint warning. Also I believe that if the setter is for a dependent property, then this mlint warning isn't present.

    To do what you're trying ("store" a value in a Dependent property), you could create a "shadow" property for c which is private and is used to store the underlying value of c. In the example below I have used c_ with the underscore to indicate that it is a shadow property.

     classdef foo
        properties
            a
            b
        end
    
        properties (Dependent = true)
            c
        end
    
        properties (Access = 'private')
            c_
        end
    
        methods
            function this = foo(c_init)
                this.a = 1;
                this.b = 1;
                this.c_ = c_init;
            end
    
            function this = set.c(this, value)
                if this.b ~= 1
                    value = 1;
                end
    
                this.c_ = value;
            end
    
            function value = get.c(this)
                value = this.c_;
            end
        end
    end
    

    Also I'm not completely sure if your post is an over-simplified version of what you're trying to do, but for the example you provided, you could very easily make c not a dependent property and just define a custom setter.

    classdef foo
        properties
            a
            b
            c
        end
    
        methods
            function this = foo(c_init)
                this.a = 1;
                this.b = 1;
                this.c = c_init;
            end
    
            function this = set.c(this, value)
                if this.b ~= 1
                    value = 1;
                end
    
                this.c = value;
            end
        end
    end