Turn Your Code Into a Package
Packages are useful for bundling code for distribution. Creating a package is often one of the final steps in the development process. If you have an existing code base that you want to bundle and distribute as a package, organize your code into one folder, create a package including any dependencies, and publish the package to a repository..
Organize Code Before Packaging
Before creating a package, move all of the files and folders that you want the package
to contain into a single folder. That folder becomes the package root folder. Consider
including a version number in the package root folder name to make it easy to differentiate
from future versions. For example, if you want to create a package named
MyPackage, name the root folder MyPackage@1.0.0.
When you develop a new version of MyPackage in the future, you can create
a copy of MyPackage@1.0.0 and update the folder name to use the new
version number.
The package root folder can contain subfolders, including special subfolders like a
resources folder. Because end users can see and explore the contents of
the package, it can be helpful to organize your code in an intuitive way. For example,
consider putting all class definition files in a subfolder named
ClassDefs and all functions in a folder named
FunctionDefs. When you create a new package, all subfolders except the
resources folder are added to the Folders property
of the new package.
If the package you are authoring uses code from another package, then you must add that other package as a dependency after creating your new package. In some cases, such as for particularly large code bases, you can to compartmentalize your code into multiple packages and then link packages as dependencies as needed.
Create New Package
Once your code is organized in a single root folder, you can create a package using the
mpmcreate
function. Any folders within the specified root folder become member folders of the new
package. If the root folder does not contain a resources folder, the
function creates one. The function adds the package definition file mpackage.json
to the resources folder.
For example, create a package named MyPackage. Specifying an output
for mpmcreate creates a matlab.mpm.Package object for your package.
pkg = mpmcreate("MyPackage","C:\MyCode\MyPackage@1.0.0");
When you create a package using mpmcreate, the package is
installed in place. In other words, the installed package files and
folders are located in the specified folder, not in the default installation area. This type
of installation makes it easier to manage package files and folders before publishing your
package for distribution.
Newly created packages have their Editable property set to
true. So, you can change package properties, and the MATLAB Package
manager updates the package definition file accordingly. While the
Editable property is true, you can edit package
metadata by changing the properties of the package object or by manually editing the
mpackage.json file in the resources folder.
Identify and Add Dependencies
If your package uses code from a different package, then you must add that package as a
dependency. When MATLAB Package Manager installs a package, it installs dependencies as
well. You can add dependencies to a package using the addDependency
function, and the MATLAB Package Manager updates package object and the package definition
file accordingly.
For example, add the package MyOtherPackage as a dependency of
MyPackage.
addDependency(pkg,"MyOtherPackage")You can remove dependencies from a package and its definition file by using the
removeDependency function. The function removes dependencies from the package
but does not uninstall them. You can update the version of an existing dependency by using
the updateDependency function.
Add Files and Manage Package Subfolders
You can add code to an existing package by placing the additional files in the root
folder or in subfolders. To add new subfolders to a package, use the addFolder
function, and the MATLAB Package Manager updates the package object and the package
definition file accordingly.
For example, create a subfolder in MyPackage named
NewFolder and add it to the package.
mkdir("MyPackage@1.0.0/NewFolder") addFolder(pkg,"NewFolder")
You can remove member folders from the package and its definition file by using the
removeFolder
function. The function removes the specified folders from mpackage.json
but does not delete the folders or their contents. Subfolders in a package that are not
included in the package definition file are not added to the path when the package is
installed.
Package subfolders can be designated to support
Java by setting the Languages property of the folder. For additional
information, see addFolder. (since R2025a)
Edit Package Information
Prepare your package for distribution by setting the package information properties. These properties can make it easier for users to discover your package, know the package's purpose, and provide information on how to use your package.
The MATLAB Package Manager automatically updates the package definition file mpackage.json
according to the changes you make to the package object properties. For example, add a
description to MyPackage by editing the Description
property on the package object pkg. Be sure that the
pkg object has an Editable property value of
true. Alternatively, you can edit metadata directly by modifying the
package definition file.
if pkg.Editable pkg.Description = "Here is my package description."; end
Package Information
Set package information properties to support the discoverability of your package.
When looking for packages by keyword using mpmsearch and
mpmlist,
MATLAB searches the following properties:
Name— Name of the package. The package name must be a valid MATLAB identifier.DisplayName— Displayed package name. This name does not need to be a valid MATLAB identifier. It can include spaces, special characters, and Unicode characters to enhance readability and provide a better understanding of the package's purpose.FormerNames— Previous package names. Use this property to allow users to search for any previous names of your package and to support backward compatibility for future versions.Summary— Brief overview of the package and its purpose.Description— Short description of how to use the package. For long descriptions, consider including areadmefile in your package and use theDescriptionproperty to direct users to this file.
Provider Information
Use the Provider property of the package to give users information
about where the package is coming from. This information is stored as a matlab.mpm.Provider object and can optionally include any of the following
information:
Name— Provider nameOrganization— Provider organizationEmail— Provider emailURL— Provider website
Release Compatibility
Use the ReleaseCompatibility property of the package to specify
which MATLAB releases your package supports. You can specify the
ReleaseCompatibility property as the full name of a MATLAB® release (for example, "R2025b") or as a semantic version
range (for example, ">25.2.0"). For more information on semantic
version and MATLAB releases, see Distinguish Between Packages Using Package Identifiers.
Publish Package to Repository
Once your package is ready for distribution, the final step is to make it available to
end users by publishing your package to a repository. A MATLAB package repository is a place where packages are stored and distributed. End
users can search for and install the package from the repository. When searching for a
package using mpmsearch or
installing a package using mpminstall, the
MATLAB Package Manager finds packages in known repositories.
For additional information on setting up and distributing packages through folder-based repositories, see Distribute Packages Using Folder-Based Repositories.
See Also
Objects
Functions
mpmcreate|mpminstall|mpmuninstall|mpmsearch|mpmlist|mpmAddRepository|mpmListRepositories|mpmRemoveRepository