Pablo Riera
Pablo Riera

Reputation: 359

Understanding MATLAB class properties

Considering this example from the MATLAB Help.

This example, besides having syntax problems, does not seams to work for me. I don't know if is a version issue, I'm using R2013a.

classdef MyClass
   properties (Constant = true)
      X = pi/180;
   end
   properties
      PropA = sin(X*MyClass.getAngle([1 0],[0 1]);
   end

   methods (Static = true)
      function r = getAngle(vx,vy)

      end
   end
end

It says

Undefined function or variable 'X'. Error in MyClass (line 1) classdef MyClass

I can fix it by adding MyClass.X, but I don't know if this was the purpose.

Upvotes: 2

Views: 140

Answers (1)

chappjc
chappjc

Reputation: 30579

That MathWorks example is all messed up. The intention was probably to have it written like this:

classdef MyClass
    properties (Constant = true)
        Deg2Rad = pi/180;
    end
    properties
        PropA = sin(MyClass.Deg2Rad*MyClass.getAngle([1 0],[0 1]));
    end

    methods (Static = true)
        function r = getAngle(vx,vy)
            r = atan2(vy,vx)/MyClass.Deg2Rad;
        end
    end
end

I guess the point is to demonstrate a static method and constant property:

>> MyClass.getAngle(1,sqrt(3))
ans =
   60.0000
>> MyClass.getAngle(sqrt(3),1)
ans =
   30.0000
>> MyClass.getAngle(0,1)
ans =
    90

Upvotes: 5

Related Questions