主要内容

Develop and Share Software in MATLAB

R2026b

The recommended workflow for software development in MATLAB® includes organizing your code, tracking changes, packaging your work, and sharing it with others. Built-in MATLAB development tools support each step in this workflow:

  1. Create and Configure a TOML Project — Organize your files, define dependencies, and establish a consistent project structure.

  2. Set Up Source Control — Track changes, manage versions, and collaborate with others.

  3. Build a Package — Prepare your code for distribution, package all necessary files, and specify required dependencies to install with your package.

  4. Upload to File Exchange or Other Package Repository — Publish your finished package so that others can discover and use it.

  5. Install Shared Package — Download and install a published package from File Exchange or other package repository.

Create and Configure a TOML Project

Projects provide a structured environment for your development work. Create a MATLAB project using the TOML format to organize files and manage the path while you develop your code. The TOML format represents project metadata, such as file organization and path settings, in a single human-readable text file. Using a TOML project helps you:

  • Manage the MATLAB path — Projects automatically add and remove folders from the path when you open and close the project, eliminating manual path setup.

  • Enforce environment consistency — Startup and shutdown tasks create a consistent environment with required dependencies installed for all team members.

  • Analyze dependencies — The Dependency Analyzer identifies which files, functions, packages, and MathWorks® products your code requires.

  • Organize large code bases — Labels, shortcuts, package dependencies, and referenced projects let you scale from a simple utility to a multi-component system without losing structure.

For more information, see Create Projects.

Create Project Interactively

Starting in R2026b, you can select the one-file-metadata TOML format as the definition file type of your project. To change the default metadata format, on the Home tab, in the Environment section, click Settings. Select MATLAB > Project, and in the New Projects section, set Project definition files to matlab.toml.

Before you create a project, ensure your code is organized in a single root folder with subfolders for source code, tests, and examples. To create a project from an existing folder, follow these steps:

  1. On the Home tab, select New > Project. Alternatively, click New Project from the Project panel.

    If you do not have the Project panel in the sidebar, add it using the Open more panels button three-dot icon in the sidebar.

  2. In the Create Project dialog box, enter a project name, select the existing folder you want to create the project in, and click OK.

If you have an existing project, you can convert it into the TOML format. For information on converting the project, see Convert Existing Projects to TOML Format.

Create Project Programmatically

You can create and configure a project from the MATLAB Command Window. For example, create a TOML-based project from the MyProject folder. Add src and utils folders to the project path, and add a startup file.

proj = matlab.project.createProject(Name="MyProject", ...
                                    Folder="C:\MyProject", ...
                                    DefinitionFilesType="Toml");

addPath(proj,"src");
addPath(proj,"utils");

addStartupFile(proj,"startup.m");

Set Up Source Control

Initialize a Git™ repository and connect it to a remote for collaboration. Integrating source control directly in your MATLAB project allows you to:

  • Track every change — See exactly what changed, when, and why. Revert to any previous state if something breaks.

  • Manage versions and collaborate — Work with multiple developers on the same code base using branches without overwriting each other's work.

  • Perform code reviews — Push changes to a remote for example, on GitHub, GitLab, or Bitbucket to use pull requests and peer review before merging.

  • Support CI/CD — A remote Git repository is the foundation for automated testing and deployment pipelines.

  • Stay in MATLAB — The built-in Source Control panel lets you commit, push, pull, branch, and resolve conflicts without leaving MATLAB.

Interactively Add Your Project to Git Source Control

To add your project to a Git repository interactively, follow these steps:

  1. Open the project. On the Project tab, in the Source Control section, click Initialize Git Repository.

  2. To connect a remote Git repository, in the Source Control section, click Add Remote. Enter the URL of your remote Git repository (for example, a GitHub repository).

  3. In the Source Control panel, click Commit and enter a commit message.

Programmatically Add Git Source Control

You can initialize and configure Git from the MATLAB Command Window. For example, from a project root folder, initialize a Git repository, add the project files, and commit them with a message. Then connect a remote Git repository and push the project files.

repo = gitinit;
add(repo,".");
commit(repo,Message="Initial commit");

addRemote(repo,"https://github.com/lrm29/testit.git");
push(repo,Remote="origin")

For more information, see Source Control Integration in MATLAB.

Build a Package

When your code is ready for distribution, package your project so that other users can install your code with a single command or click. Packaging your code helps you manage distribution, dependencies, and versioning:

  • Install in one step — Users can install your package in a single step, for example, by using the mpminstall function, by double-clicking a package file (.mltbx), or by using the Add-Ons panel. MATLAB Package Manager handles all files, dependencies, and metadata automatically.

  • Manage dependencies — Specify which other packages your code requires. MATLAB Package Manager resolves and installs dependencies automatically.

  • Version your package — Users install the exact version you tested, which helps ensure that your software behaves consistently across machines. Specify a semantic version for your package so that users know exactly which release they have and can update the package when a new version is available.

  • Improve discoverability — Define package metadata (such as the name, summary, and tags) to make your code searchable in repositories and on File Exchange.

Interactively Build a Package

To build a package from your project interactively, follow these steps:

  1. On the Project tab, in the Tools section, click the Build Package button. The package task opens.

  2. Enter the package name, author, version, and additional information in the corresponding fields.

  3. In the Package Folder section, verify that the expected folders and files are included. Remove test folders or internal files that you do not want to distribute.

  4. Click Build Package to generate a package file (.mltbx) in the specified output location.

For more information, see Interactively Create and Share Packages.

Programmatically Build a Package File (.mltbx)

To programmatically create a package file in your project folder, follow these steps:

  1. Gather the files and folders that you want to share into a single folder that will become the package root folder.

  2. Create a matlab.addons.toolbox.ToolboxOptions object and specify the matlab.toml file for your project.

    projFile = "C:\MyProject\matlab.toml";
    opts = matlab.addons.toolbox.ToolboxOptions(projFile);
  3. Set the properties of your ToolboxOptions object to configure the package options.

    opts.ToolboxName = "My Package";
    
    opts.ToolboxFiles = "Export";
     
    opts.SupportedPlatforms.Win64 = true;
    opts.SupportedPlatforms.Mac = false;
    opts.SupportedPlatforms.Glnxa64 = true;
    opts.SupportedPlatforms.MatlabOnline = true;
    
    opts.MinimumMatlabRelease = "R2026b";
    opts.MaximumMatlabRelease = "";
    
    opts.PackageDependencies = matlab.mpm.Dependency( ...
               "Gui Layout Toolbox", ...
               ">=4.2.0", ... 
               "e5af5a78-4a80-11e4-9553-005056977bd0");
    
    opts.RequiredAdditionalSoftware = ...
        struct("Name", "Dataset", ...
               "Platform", "common", ...
               "DownloadURL", "https://github.com/myusername/myproject/data.zip", ...
               "LicenseURL", "https://github.com/myusername/myproject/LICENSE");
    
  4. Create a package file using matlab.addons.toolbox.packageToolbox. The resulting package file (.mltbx) appears in the project root folder.

    matlab.addons.toolbox.packageToolbox(opts);

Automate Package Creation Using the Build Tool

If you use the MATLAB build tool to automate your builds, you can create a package as a build task by adding a PackageTask instance to your buildfile.m file. This approach integrates packaging with other build steps, such as testing and code analysis, in a standard location. For example:

function plan = buildfile
plan = buildplan(localfunctions);

plan("check") = matlab.buildtool.tasks.CodeIssuesTask;
plan("test") = matlab.buildtool.tasks.TestTask;
plan("package") = matlab.buildtool.tasks.PackageTask;

plan("package").Dependencies = ["check","test"];

plan.DefaultTasks = "package";
end

On the Project tab, click Run Build to run the default "package" task, or use the drop-down list to run the check or test tasks. Alternatively, you can run the "package" task from the Command Window.

buildtool package

The build tool runs the "check" and "test" tasks first, and then creates the package only if all preceding tasks pass.

For more information about the build tool, see Overview of MATLAB Build Tool.

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 find and download it. MathWorks File Exchange is the official repository supported by MathWorks and the primary community hub for sharing MATLAB code. Publishing to File Exchange gives you:

  • Visibility — Thousands of MATLAB users browse and search File Exchange daily. Your package becomes discoverable by keyword, category, and tag, in addition to package name and summary.

  • Availability — Packages shared to File Exchange are available for download and can be added as dependencies to other packages and projects.

  • Credibility — Ratings, reviews, and download counts provide social proof that builds trust with potential users.

  • Community feedback — Comments and questions help you improve your package and understand how people use it.

  • Automatic updates — When linked to a GitHub repository, File Exchange detects new releases and updates the listing without manual re-upload.

  • One-step install — Users can install your package in several ways, including directly from the File Exchange page, from the MATLAB Add-Ons panel, or programmatically using the mpminstall function.

Link GitHub Repository to File Exchange

If your project is 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 file (.mltbx) 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 .mltbx file 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 .mltbx file.

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.

Install Shared Package

If your package is on File Exchange, end users can find and download the package in several ways:

  • From the File Exchange page

  • From the Add-Ons panel

  • From the dependencies interface in a TOML-based project

  • Using the mpminstall function

For example, from the File Exchange search box, users can type keywords, tags, or a name to find the package. From the package page, end users can click the Download button to download the package and then install it. MATLAB Package Manager automatically handles package files, dependencies, and metadata.

For more information, see Find and Install Packages Programmatically.

See Also

Topics