access separate function file from handleclass

1 view (last 30 days)
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

Accepted Answer

Guillaume
Guillaume on 26 Jun 2019
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 Comments
Steven Lord
Steven Lord on 26 Jun 2019
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 on 26 Jun 2019
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Construct and Work with Object Arrays in Help Center and File Exchange

Products


Release

R2015a

Community Treasure Hunt

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

Start Hunting!