Put class definition code in files that have the .m
extension. The name of
the file must be the same as the name of the class followed by the
.m
extension.
For information on the code that defines a class, see Class Components.
There are two ways to create folders that contain class-definition files:
Path folder — a folder that is on the MATLAB® path.
Class folder — a folder that is in a path
folder and is named with the @
character and the class
name. For example:
@MyClass
Class folders are not directly on the MATLAB path. The path folder that contains the class folder is on the MATLAB path.
There are two ways to specify classes with respect to files and folders:
Create a single, self-contained class definition file in a path folder or a class folder
Define a class in multiple files, which requires you to use a class folder inside a path folder
Create a single, self-contained class definition file in a folder
on the MATLAB® path. The name of the file must match the class
(and constructor) name and must have the .m
extension.
Define the class entirely in this file. You can put other single-file
classes in this folder.
The following diagram shows an example of this folder organization. pathfolder
is
a folder on the MATLAB path.
If you use multiple files to define a class, put all the class-definition
files (the file containing the classdef
and all
class method files) in a single @ClassName
folder.
That class folder must be inside a folder that is on the MATLAB path.
You can define only one class in a class folder.
A path folder can contain classes defined in both class folders and single files without a class folder.
The parent folder to a package folder is on the MATLAB path,
but the package folder is not. Package folders (which always begin
with a +
character) can contain multiple class
definitions, package-scoped functions, and other packages. A package
folder defines a new name space in which you can reuse class names.
Use the package name to refer to classes and functions defined in
package folders (for example, packagefld1.ClassNameA()
, packagefld2.packageFunction()
).