access separate function file from handleclass

조회 수: 1 (최근 30일)
Dagmar
Dagmar 2019년 6월 26일
댓글: Guillaume 2019년 6월 26일
Hi,
I'm trying to use separate function files to be accessed from a handleclass.
Here's the class that I defined, including two methods that are defined in separate function files (located in the @myclass directory).
classdef myclass <handle
properties
factor;
x;
y;
end
methods
me = myfactor(me)
out = myfactor2(x, factor)
function me = myclass(x)
me.x = x;
end
function me = setfactor(me, f)
me.factor = f;
end
function me = inclassfactor(me)
me.y = me.x.^me.factor;
end
function me = testfactor(me)
me = myfactor(me);
end
function me = testmyfactor2(me)
me.y = myfactor2(me.x, me.factor);
end
function me = testfeval(me)
me = feval('myfactor', me);
end
end
end
The functions defined are all the same (x.^factor).
I then run a small test:
X = [0 1 2 3];
test = myclass(X);
test.setfactor(2);
test.inclassfactor
test.setfactor(1);
test.testfactor;
test.setfactor(2);
test.testfeval;
test.setfactor(3);
test.testmyfactor2;
it gives an error for the last line of code, myfactor2 is an undefined function.
I know that I can use myfactor as a suitable 'workaround', but I would like to understand why myfactor2 is not recognized.
Can anyone explain to me what I'm missing/not understanding?
Thanks in advance,
Dagmar

채택된 답변

Guillaume
Guillaume 2019년 6월 26일
Any (non-static) class method must have as one of its input argument (not necessarily the first) an instance of the class. Therefore, if
out = myfactor2(x, factor)
is a class method, either x or factor must be an instance of myclass. If you call
myfactor2(me.x, me.factor)
then matlab won't even look in the class definition to find its implementation since neither input is of type myclass.
If myfactor2 is not meant to operate on instances of the class, then it needs to be either a free (possibly private or local) function or a static method of the class.
  댓글 수: 4
Steven Lord
Steven Lord 2019년 6월 26일
In my experience, generally if a class method does something with the data stored in the object it's more likely to return an instance of the object. As examples the arithmetic operators (plus, minus, times, etc.) and data analysis functions (sum, prod, min, max, etc.) probably return an instance of the class, as do methods like reshape and sort. There are exceptions, like the relational operators (==, >, etc.) that should return logical arrays even though they operate on the data.
Class methods that look at the object itself rather than the data probably won't return an instance of the object. Probably the most common example of this is the size function. Most size methods will return a double precision row vector. Functions like ismatrix, isreal, etc. also probably will return logical arrays rather than object instances.
Guillaume
Guillaume 2019년 6월 26일
Yes, but the class methods that do something with the data stored in the object return a different instance of the class, not the instance that was passed as input. plus returns a different number than the two inputs. (In any case, plus, etc. is more suited for a value class).
Certainly, the
function me = inclassfactor(me)
me.y = me.x.^me.factor;
end
doesn't need to return me. It's the same instance that was passed as input. The test code in the question never uses that return value. To me, it looks like the OP is confusing value and handle classes.

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by