Why must a function in the methods block of a class have an input argument?

39 次查看(过去 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

采纳的回答

Guillaume
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
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
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)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by