Main Content

Programmatically Turn Your Code Into a Package

R2026b

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.

For information about interactively creating a package file (.mltbx), see Interactively Create and Share Packages.

When developing code in MATLAB®, consider using projects to help organize code and track dependencies. For more information on using projects and other development tools to publish a package, see Develop and Share Software in MATLAB.

Organize Code Before Packaging

Before creating a package, move all 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 differentiate it 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 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 a package definition file mpackage.json to the resources folder.

For example, create a package named MyPackage. Specifying an output for mpmcreate returns 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 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 MATLAB Package Manager updates the 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 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 the package object and 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.

You can designate package subfolders to support Java by setting the Languages property of the folders. 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.

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 summary to MyPackage by editing the Summary 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.Summary = "Here is my package summary.";
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 for better readability.

  • FormerNames — Previous package names. Use this property to enable 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.

  • Readme — Readme file containing instructions on how to use the package. (since R2026b)

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 name

  • Organization — Provider organization

  • Email — Provider email

  • URL — 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 versions and MATLAB releases, see Distinguish Between Packages Using Package Identifiers.

Upload to File Exchange or Other Package Repository

Once your package is ready to be shared, you can upload it to a package repository so that others can discover and use it. MathWorks File Exchange is the official repository supported by MathWorks and the primary community hub for sharing MATLAB code.

Link GitHub Repository to File Exchange

If your package code is in a project hosted on GitHub, you can link the GitHub repository to File Exchange.

  1. Go to the MathWorks File Exchange and sign in with your MathWorks Account.

  2. Click Publish your code.

  3. Select GitHub as the source.

  4. Configure the listing metadata.

  5. Click Publish to submit.

To release an updated version of your package, create a GitHub release or push a commit. File Exchange detects the update and refreshes the listing automatically.

Manual Upload to File Exchange

You can upload a package directly to File Exchange.

  1. Go to the MathWorks File Exchange and sign in with your MathWorks Account.

  2. Click Publish your code.

  3. Select Publish to File Exchange > Upload File(s).

  4. Browse and select the package you want to upload.

  5. Configure the listing metadata.

  6. Click Publish to submit.

To update an existing submission, navigate to your contribution page, click Edit, and upload the new package version.

Consider the following best practices to make your package more discoverable and to help your end users understand its functionality:

  • Write a concise summary (one to two sentences) that explains what the package does and who it is for.

  • Include a screenshot or graphic that demonstrates a key feature.

  • Add relevant tags so users can discover your package through search.

  • Provide a Getting Started example or link to documentation within the package.

  • Specify the minimum MATLAB release and required package dependencies in the listing description.

Upload File to Folder-Based Repository

You can also share packages by designating a folder as a repository and adding them to that folder. End users with access to that folder can designate it as a repository using the mpmAddRepository function. For more information on using folder-based repositories, see Distribute Packages Using Folder-Based Repositories.

See Also

Objects

Functions

Topics