access separate function file from handleclass

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

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

Thanks for the quick response.
I decided to chance out = myfactor2(x, factor) into out = myfactor2(me, x, factor). Which works. It wouldn't really make sense (in my case) to create a Static method, as the function is not needed without a class instance.
By not returning the class instance back, I can also prevent overwriting the class instance from within the function (I hope).
Thanks again, Dagmar
Class methods do not have to return the instance of the class, if it's not needed. In fact, that's probably the case with the majority of class methods.
I didn't spot that the first time, but in your example, apart from the constructor, none of the methods need to return the class instance. It appear you have written your class methods as if the class was a value class, not a handle class.
However, it doesn't make sense for a class method to take both a class instance and class properties as inputs. So your my factor method should either be:
function out = myfactor2(me) %as a class method
%do something with me.x and me.factor
end
or as you had but as a private, local, free, or static function which doesn't take a class instance.
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.
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)

Products

Release

R2015a

Asked:

on 26 Jun 2019

Commented:

on 26 Jun 2019

Community Treasure Hunt

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

Start Hunting!