Main Content

Create Namespaces

Namespaces are special folders that can contain class folders, class definition files, function files, and other namespaces (referred to as inner namespaces). Using a namespace helps organize code and creates more robust names for the items contained inside it. Function and class names must be unique inside a given namespace. The functions and classes are scoped to the namespace, so you can reuse those names in a different namespace.

Namespace Folders

Define a namespace by adding the + character at the beginning of the folder name. For example, the namespace folder +mynamesp contains a class, an enumeration class, a function, an inner namespace, and a second class in a class folder inside the inner namespace.

+mynamesp
+mynamesp/MyClass.m
+mynamesp/WeekdaysEnum.m
+mynamesp/myFunction.m
+mynamesp/+innernamesp/@MySecondClass/MySecondClass.m

Note

This folder hierarchy is for example purposes only. It is not part of the MATLAB® installation. To experiment with the functionality, define these folders and code on your own path.

To be accessible to MATLAB, the parent folder of the namespace folder must be on the path. Trying to add the namespace folder itself to the path causes an error. For more information, see Namespaces and the MATLAB Path.

Define Code in Namespaces

Define members of the namespace as you would any other code. Do not include the namespace name in the definitions. For example, in the sample hierarchy shown in Namespace Folders, the definition of myFunction includes only the function name.

function z = myFunction(x)
  ...
end

Similarly, the definition of MyClass includes only the class name.

classdef MyClass
  ...
end

Reference Namespace Members

To reference most namespace members, add the namespace name to the beginning of the member name, followed by a dot. For example, this statement creates an instance of MyClass, which is contained in mynamesp. (See the sample hierarchy shown in Namespace Folders.)

obj = mynamesp.MyClass(arg1,arg2);

A call to code defined in the namespace, even if the call is inside the namespace itself, must include the namespace name. For example, if myFunction is used as a property validation function by MyClass, the function name must include the namespace.

classdef MyClass
   properties
      myProp {mynamesp.myFunction}
   end
end

To simplify outside access to namespace code, you can import code from a namespace. Calling imported code does not require the namespace name. For more information, see Import Namespace Members into Functions.

Class Folders

When a class is defined in a class folder (@-folder), the class folder name is not part of the namespace name. For example, constructing an object of MySecondClass uses this syntax.

obj = mynamesp.innernamesp.MySecondClass(arg1,arg2);

Methods

Calling nonstatic methods of a class does not require the namespace name because you have an object of the class, which is already scoped to the namespace. Calling the myMethod method of obj, an instance of MyClass follows the usual syntax.

obj.myMethod(arg)
myMethod(obj,arg)

A static method always requires the full class name and the namespace.

mynamesp.myClass.stMethod(arg)

Properties

Like nonstatic methods, you can access the public properties of an instance without using the full namespace. However, accessing a constant property of a class without a specific instance of that class requires the full namespace. For example, if MyClass defines a constant property ConstProp, access it using the full namespace.

x = mynamesp.MyClass.ConstProp;

If you already have an instance of MyClass, you can access ConstProp without using the full namespace.

y = mynamesp.MyClass;
c = y.ConstProp;

Enumerations

To reference an enumeration member in a namespace, use the full namespace name. For example, this is a possible definition of WeekdaysEnum inside mynamesp.

classdef WeekdaysEnum
   enumeration
      Monday, Tuesday, Wednesday, Thursday, Friday
   end
end

Assign the enumeration member Monday to a variable using the full namespace.

day = mynamesp.WeekdaysEnum.Monday;

Namespaces and the MATLAB Path

You must add the parent folder of the top-level namespace to the MATLAB path to call code in the namespace. Namespace members are not accessible if the parent folder is not on the MATLAB path, even if the namespace folder is the current folder.

How MATLAB Handles Duplicate Names

Namespaces with the Same Name

Namespace folders do not take precedence over other namespace folders with the same name positioned later on the path. If two or more top-level namespaces have the same name, MATLAB treats them as one namespace. If two namespaces with the same name in different path folders define the same function name, then MATLAB finds only one of these functions.

Namespace Functions and Static Methods

When a namespace function and a static method have the same name, the namespace function takes precedence over the static method. For example, path folder fldrA contains a namespace function bar, and path folder fldrB contains a static method bar of class foo.

fldrA/+foo/bar.m  % bar is a function in namespace foo
fldrB/@foo/bar.m  % bar is a static method of class foo

Use the which command to show that the namespace function has precedence.

which foo.bar
fldrA\+foo\bar.m

The internal Namespace

MathWorks® reserves the use of namespaces named internal. Code contained in namespaces named internal is intended for MathWorks use only, and use of this code is discouraged. This code is not guaranteed to work in a consistent manner from one release to the next. Any of these functions and classes can be removed from the MATLAB software in any subsequent release without notice.

Related Topics