How can I create an instance of a class in a method?

9 visualizaciones (últimos 30 días)
Maxim
Maxim el 19 de Nov. de 2022
Comentada: Steven Lord el 19 de Nov. de 2022
I have a class for my needs. It's a simple table of function points.
classdef FuncTable < handle
%TABLE Class is up to function definition table
properties
X (1,:) {mustBeNumeric}
Y (1,:) {mustBeNumeric}
end
methods
function obj = FuncTable(X,Y)
if (length(X) ~= length(Y))
throw MException('MATLAB:sizeDimensionsMustMatch', "Размеры массивов в таблице значений отличаются")
end
obj.X = X;
obj.Y = Y;
end
%some definitions here too%
end
end
What I wanna do is to define method diff for this class like this
function dt = diff(t)
dx = diff(t.X);
dy = diff(t.Y);
dt = FuncTable(dx, dy);
end
But! There are no any FuncTable function or variable in a point of view of my class.
Unrecognized function or variable 'FuncTable'.
It's bothering me. I can't find anything about this in the documentation. How can I create an instance of the class in a methods?

Respuestas (1)

Matt J
Matt J el 19 de Nov. de 2022
Editada: Matt J el 19 de Nov. de 2022
This worked fine for me.
>> t=FuncTable(rand(1,5),rand(1,5))
t =
FuncTable with properties:
X: [0.8712 0.0816 0.8790 0.7562 0.4050]
Y: [0.6144 0.6752 0.8575 0.7428 0.8519]
>> diff(t)
ans =
FuncTable with properties:
X: [-0.7896 0.7974 -0.1228 -0.3512]
Y: [0.0608 0.1824 -0.1148 0.1091]
classdef FuncTable < handle
%TABLE Class is up to function definition table
properties
X (1,:) {mustBeNumeric}
Y (1,:) {mustBeNumeric}
end
methods
function obj = FuncTable(X,Y)
if (length(X) ~= length(Y))
throw MException('MATLAB:sizeDimensionsMustMatch', "Размеры массивов в таблице значений отличаются")
end
obj.X = X;
obj.Y = Y;
end
function dt = diff(t)
dx = diff(t.X);
dy = diff(t.Y);
dt = FuncTable(dx, dy);
end
end
end
  3 comentarios
Maxim
Maxim el 19 de Nov. de 2022
Editada: Maxim el 19 de Nov. de 2022
Ok. I found an answer. Import statement is needed.
function dt = diff(t)
import common.*;
dx = diff(t.X);
dy = diff(t.Y);
dt = FuncTable(dx, dy);
end
Your answer was helpful. 10q
Steven Lord
Steven Lord el 19 de Nov. de 2022
The import function's documentation page has this caution on it:
"import PackageName.* adds the contents of the specified package name. PackageName must be followed by .*.
Avoid using this syntax, as importing the contents of packages brings an unspecified set of names into the local scope, which might conflict with names in the MATLAB workspace. One possible use for this syntax is to import a partial package name. Then when you call a function, you use a shorter package name which does not conflict with simple function names."
Your code may break without you making any change to it if any package folder named +common anywhere on the MATLAB path defines a function named diff. Those imported functions would be at level 5 on the function precedence order where the built-in diff function that you intended to call is at level 11.

Iniciar sesión para comentar.

Categorías

Más información sobre Debugging and Analysis en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by