2014-02-27 79 views
2

考慮到MATLAB幫助中的this example瞭解MATLAB類的屬性

這個例子,除了有語法問題,不接縫爲我工作。我不知道是否是版本問題,我正在使用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 

它說

未定義的函數或變量 'X'。在MyClass的錯誤(1號線) classdef MyClass的

我可以通過添加MyClass.X修復它,但我不知道這是否是目的。

+0

你應該[報告](HTTP:// WWW。 mathworks.co.uk/support/bugreports)這是一個文檔錯誤。 –

回答

5

That MathWorks example都搞砸了。其目的很可能是要把它寫成這樣:

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 

我猜的一點是要展示一個靜態方法和不變的性質:

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

謝謝。我希望有一個與C++類似的語法,在這裏你不必預先添加MyClass。到處都是。 –

+0

您應該[報告](http://www.mathworks.co.uk/support/bugreports)這是一個文檔錯誤。 –