Defined method can not be invoked.

조회 수: 2 (최근 30일)
younghwa park
younghwa park 2023년 1월 5일
편집: per isakson 2023년 1월 17일
I want to extend ss object. But, the methods can not de invoked in the file.
Am I using wrong way?
Here is my object
classdef ssSpring < ss
%SSSPRING Summary of this class goes here
% Detailed explanation goes here
properties (Access = public)
x0;
end
methods
function sys = ssSpring()
sys.A=[0,0,0,1,0,0;0,0,0,0,1,0;0,0,0,0,0,1;-1,1,0,0,0,0;1,-2,1,0,0,0;0,1,-1,0,0,0];
sys.B=[0;0;0;1;0;0];
sys.C=[0,0,1,0,0,0];
sys.D=[0];
sys.x0=[0.323811195852370;0.430986087686128;0.764897800510776;0.996462675076854;0.265018285144099;0.792067001328556];
end
end
methods (Access=public)
function b = printa(a)
b=a;
end
end
end
  댓글 수: 2
Walter Roberson
Walter Roberson 2023년 1월 5일
Which method cannot be invoked in what file?
younghwa park
younghwa park 2023년 1월 5일
Thanks for seeing my quesiton!
sys=ssSpring()
sys.printa(1)
returns errors.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2023년 1월 5일
The first parameter to a non-static non-constructor class method is always the class object that is being operated on. That would be sys in the context of your code. You then attempt to pass a second parameter with value 1 but the method is only defined to accept one parameter.
sys.printa(1)
is the same as
printa(sys, 1)
  댓글 수: 2
Steven Lord
Steven Lord 2023년 1월 5일
The first parameter to a non-static non-constructor class method is always the class object that is being operated on.
That's not always correct. At least one of the inputs to a non-Static and non-constructor class method must be an instance of the class, but it does not necessarily need to be the first input. As an example both of the following calls would use the plus method of the sym class even those in the second case the first input is the double value 1.
syms x
which plus(x,1)
/MATLAB/toolbox/symbolic/symbolic/@sym/plus.m % sym method
which plus(1,x)
/MATLAB/toolbox/symbolic/symbolic/@sym/plus.m % sym method
If both the inputs are objects, the rules for which class's method gets called are listed here. When InferiorClasses are involved, the first object is not always the one whose method gets called.
But your point about the method needing to be defined to accept two inputs rather than just one is correct and is the root cause of the error @younghwa park received.
Walter Roberson
Walter Roberson 2023년 1월 5일
Good point, @Steven Lord

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Construct and Work with Object Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by