MATLAB: Do I receive an error when I try to instantiate a class whos class definition file resides in a sub-directory inside a package

MATLAB

I have created the following directory structure:
ParentDirectory
+mypack
testClass.m
Demos
demoClass.m
I often like to put a folder called Demos in a package directory, where demonstrations using the class or classes in that package folder are placed. Often the demonstrations are scripts, but sometimes they are themsleves classes.
'ParentDirectory' is on my MATLAB path and I import the package:
import mypack.*
If I navigate to the Demos folder, and then try to instantiate an object of 'demoClass':
obj = demoClass
I see the error
??? Error using ==> demoClass
The class 'demoClass' contains a parse error or cannot be found on
MATLAB's search path, possibly shadowed by another file with the
same name.
However, it works the second time.

Best Answer

The error occurs because packages may contain class directories, functions, and other packages, but not sub-directories. To work around the issue, create a package from the 'Demos' sub-directory.
However, package directories cannot be added to the MATLAB path, therefore, to instantiate an object of 'demoClass', either import the 'Demos' package, or use the fully qualified name of the class:
import mypack.Demos.*
obj = demoClass
OR
obj = mypack.Demos.demoClass