| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
| On this page… |
|---|
Usually, you create an object by calling a function designed for the purpose of creating that specific class of object. For example, the following code creates a timeseries object and assigns it to the variable tsboj:
load count.dat % Load some data tsobj = timeseries(count(:,1),1:24,'Name','Data1');
The timeseries method creates an object and initializes its data with the values specified as arguments. Classes that create objects define a special method whose purpose is to create objects of the class. This function has the same name as the class and is called the class constructor.
However, in some cases, you might create objects by calling other functions or even using a GUI. For example, a try-catch block can return an MException object that contains information about a specific error condition. In this case, you do not explicitly create the object, rather it is returned by the catch statement (see Accessing Object Data for an example).
A package is a container that provides a logical grouping for class and function definitions. The class and function names within a given package must be unique, but can be reused in other packages. Packages are directories that begin with the + character.
If a package directory contains a class definition, then you must use the package name when calling the class constructor. For example, this statement creates a Map object, whose class definition file is in a directory in the containers package:
mapobj = containers.Map({'rose','bicycle'},{'flower','machine'});
You need to use the package name to refer to:
Class constructors (e.g., containers.Map), which you call to create an object
Static methods (methods that do not require an object of the class as an argument)
Package functions (functions defined in the package)
However, because MATLAB uses the class of an object to determine which ordinary method to call, you do not need to use the package name in conjunction with object references. For example, suppose you have the following directory structure:
pathdirectory/+packagename/@ClassName/ClassName.m pathdirectory/+packagename/@ClassName/staticMethodName.m pathdirectory/+packagename/functionName.m
In the following examples, obj is the object you are creating.
% Create object of ClassName obj = packagename.ClassName(...); % Call methodName obj.methodName(...); % Set or get the value of property PropertyName obj.PropertyName = x; x = obj.PropertyName; % Call static method staticMethodName packagename.ClassName.staticMethodName(...); % Call package function functionName packagename.functionName(...)
If a package directory contains a class definition file, then consider the package name as part of the class name. Wherever you need to use the class name, include the package name. For example, containers.Map is the full class name of the Map class.
See the object's user documentation for the syntax you need to use to create objects.
See Organizing Classes in Directories and Scoping Classes with Packages for more information on the use of packages.
See Importing Classes for information on importing packages into functions.
![]() | Key Object Concepts | Accessing Object Data | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |