Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

Class Files and Folders

Class Definition Files

Place class definition code in .m files, similarly to how you define function files. The name of the file is the name of the class followed by the .m extension. For information on the code that defines a class, see Class Components.

Options for Class Folders

There are two ways 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 name 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.

Options for Class Files

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

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.

Distribute the Class Definition to Multiple Files

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.

Group Classes with Package Folders

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()).

More About

Was this topic helpful?