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

조회 수: 9 (최근 30일)
Maxim
Maxim 2022년 11월 19일
댓글: Steven Lord 2022년 11월 19일
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?

답변 (1개)

Matt J
Matt J 2022년 11월 19일
편집: Matt J 2022년 11월 19일
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
Maxim
Maxim 2022년 11월 19일
편집: Maxim 2022년 11월 19일
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 2022년 11월 19일
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.

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

카테고리

Help CenterFile Exchange에서 Debugging and Analysis에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by