MATLAB Answers

0

Are .m files in a subfolder of a class folder considered methods of that class?

I am creating a class using the class folder approach. I understand from the MATLAB documentation that any .m file in my class folder will be treated as a method of the class.
I would like to create a subfolder within my class folder. Will .m files within this subfolder also be treated as methods of the class?
Ex. @myClass > subfolder > myMethod.m

1 Answer

Answer by MathWorks Support Team on 19 Jun 2019
 Accepted Answer

Files in a subfolder of a class folder are not considered methods of the class.
Please refer to the following example:
@myClass > myClass.m:
classdef myClass
methods
function classObj = myClass()
classObj.method1()
classObj.method2()
end
end
end
@myClass > method1.m:
function method1(classObj)
disp('method in class folder')
end
@myClass > subfolder > method2.m:
function method2(classObj)
disp('method in subfolder')
end
-----
>> m = myClass
method in class folder
Unrecognized method, property or field 'method2' for class 'myClass'.
Error in myClass (line 5)
classobj.method2()
As you can see, 'method1' is part of the class, but 'method2', which is in a subfolder of '@myClass', is not treated as a method of the class.
For more details about using class folders in MATLAB, please see the following documentation page:

  1 Comment

Except for a private subfolder?

Sign in to comment.