Defined method can not be invoked.

1 visualización (últimos 30 días)
younghwa park
younghwa park el 5 de En. de 2023
Editada: per isakson el 17 de En. de 2023
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 comentarios
Walter Roberson
Walter Roberson el 5 de En. de 2023
Which method cannot be invoked in what file?
younghwa park
younghwa park el 5 de En. de 2023
Thanks for seeing my quesiton!
sys=ssSpring()
sys.printa(1)
returns errors.

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 5 de En. de 2023
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 comentarios
Steven Lord
Steven Lord el 5 de En. de 2023
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 el 5 de En. de 2023
Good point, @Steven Lord

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Construct and Work with Object Arrays en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by