Why must a function in the methods block of a class have an input argument?
40 ビュー (過去 30 日間)
古いコメントを表示
I have gotten my constructor to work but now I have a function doing some calculations using property parameters. However although I can use the constructor the function (below doesn't work). If I invoke b.stiffness() I receive a message a function must have an input argument. The Mathworks documentation shows functions without arguments needn't use "()" I tried that. Same message. I tried enclosing this function in a methods (Static = true) ... end block then I get a complaint about it can't use "obj." So I tried removing that thinking I could just access the properties directly (as in C++) but this doesn't work. So...why must a function in a Matlab class have an input argument?? Any help appreciated. peter
methods(Static = true)
function stiffnessk
%calculate the length
obj.L = sqrt( (obj.p2(1) - obj.p1(1))^2 + (obj.p2(2) - obj.p1(1))^2 );
obj.sprk = obj.crossXarea * obj.E / obj.L;
obj.elemangle = atan2(obj.p2(2) - obj.p1(2), obj.p2(1) - obj.p1(1));
c2 = ( cos(obj.elemangle) )^2; s2 = ( sin(obj.elemangle) )^2;
cs = cos(obj.elemangle) * sin(obj.elemangle);
obj.K(1,1) = c2; obj.K(1,2) = cs; obj.K(1,3) = -c2; obj.K(1,4) = -cs;
obj.K(2,1) = cs; obj.K(2,2) = s2; obj.K(2,3) = -cs; obj.K(2,4) = -s2;
obj.K(3,1) = -c2; obj.K(3,2) = -cs; obj.K(3,3) = c2; obj.K(3,4) = cs;
obj.K(4,1) = -cs; obj.K(4,2) = -s2; obj.K(4,3) = cs; obj.K(4,4) = s2;
end
end
0 件のコメント
採用された回答
Guillaume
2015 年 2 月 21 日
I believe it's explained somewhere in the doc about classes.
Whereas in C++ the object is passed implicitly to the methods of a class (the this parameter, that the compiler adds to the function signature), in matlab you have to explicitly pass it as the first argument of the function:
Therefore your function signature must be:
function stiffnessk(obj)
...
end
Personally, because I'm used to the this of C++ and C# I use
function stiffnessk(this)
this.L = ...
...
end
But, it's just a variable name like any other in matlab.
Static methods (just like in C++) belong to the class not to instances of the class. They don't and can't operate on objects. It's thus invalid to access object properties (like obj.L) within them.
Finally, in matlab, if you change any property of the object within a method, the class needs to be a handle class for it to work. For a value class, you need to return a new object from the method.
1 件のコメント
Luke Perry
2019 年 7 月 25 日
Just to comment on Guillaume's answer if it is not implicitly understood. Constructor functions do not need any parameters passed to them if necessary.
For instance, I can make the following class:
classdef MyClass <handle
methods
function self = MyClass
end
end
end
and it is perfectly valid to create an object from this class by calling that function:
>>a=MyClass
a =
MyClass with no properties.
However, for all other functions, do as Guillaume suggested.
その他の回答 (1 件)
David Young
2015 年 2 月 21 日
編集済み: David Young
2015 年 2 月 21 日
The method declaration needs to start
function stiffnessk(obj)
The argument it needs is the object for which it is being called. It should not be declared static.
The call
b.stiffnessk()
is equivalent to
stiffnessk(b)
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!