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

채택된 답변

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