MATLAB: Access separate function file from handleclass

classeshandle classMATLABoopseparate function files

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

Best Answer

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.