Main Content

Create and Manage Packages

You can use the MATLAB® Package Manager to create packages. You can also modify packages and manage their folders and dependencies.

Create Package

You can create a new package using the mpmcreate function. For example, to create a package named MyPackage. Specify the package root folder. Any folders within the specified root folder become member folders of the new package. If the specified location does not exist, the function creates a new folder and an empty 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.

pkg = mpmcreate("MyPackage","C:\MyCode\MyPackage");

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.

Newly created packages have their Editable property set to true. So, you can change package properties, and the package definition file updates accordingly.

Modify Installed Package

By default, installed packages are not editable. If you want to be able to modify an installed package, specify the Editable name-value argument as true when you install the package using the mpminstall function. Alternatively, if you specify Authoring=true, then the function installs the package in place with the Editable property set to true.

pkg = mpminstall("MyPackage",Authoring=true);

For additional information about finding and installing packages, see Find and Install Packages.

When you make changes to an installed package, it is recommended that you update the version of the package to support backward compatibility. When installing a package, the mpminstall function copies the metadata stored in the package definition file mpackage.json and stores it. Changes to mpackage.json do not affect installed packages unless the package is in editable mode.

Edit Package Information

A package must be in editable mode for MATLAB to recognize changes to the information stored in its package definition file and member folders. By default, packages are created in editable mode. Uninstalled packages are in editable mode except when they are in a repository. That is, the Editable property of uninstalled packages is always true, except for packages in repositories. The Editable property of uninstalled packages in a repository is always false. Packages are not installed in editable mode by default. However, you can specify a package to be editable when you install it with mpminstall by specifying the Authoring name-value argument as true.

When a package is in editable mode, the package definition file can change when you change the properties of the matlab.mpm.Package object corresponding to that 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.

pkg.Description = "This is my first package"
pkg = 

  Package with properties:

   Package Definition
                     Name: "MyPackage"
              DisplayName: "MyPackage"
                  Version: 1.0.0 (1×1 Version)
                  Summary: ""
              Description: "This is my first Package"
                 Provider: <missing>
                  Folders: [1×0 PackageFolder] (1×0 PackageFolder)
             Dependencies: ""
     ReleaseCompatibility: "*"
              FormerNames: ""
                       ID: "cabe1581-671d-40a2-8137-2b17847a9c65"

   Package Installation
                Installed: 1
                 Editable: 1
    InstalledAsDependency: 0
              PackageRoot: "C:\MyCode\MyPackage"
    InstalledDependencies: ""
      MissingDependencies: ""

   Repository
               Repository: [0×0 Repository]

  help MyPackage

Manage Package Code and Subfolders

You can add code to a package by placing the files in the root folder or in subfolders. Then, add subfolders to a package as member folders using the addFolder function so that the MATLAB Package Manager updates the package definition file accordingly.

For example, create a subfolder in MyPackage named NewFolder and add it to the package.

mkdir("MyPackage/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 folder from mpackage.json but does not delete the folder or its 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.

Manage Package Dependencies

Package code must be contained within the package root folder to be part of the package. However, some packages depend on code in other packages. These other packages are called dependencies. You can add dependencies to a package using the addDependency function, and the MATLAB Package Manager updates the package definition file accordingly. When installing a package, the MATLAB Package Manager also installs package dependencies by default.

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.

If you want to make MyPackage available to other users, add it to a repository. For more information about sharing packages, see Distribute Packages Using Folder-Based Repositories.

See Also

Objects

Functions

Related Topics