Create and Reference a Project Programmatically

This example shows how to programatically create a new project and add it as a reference project in your main project. It covers how to create a project from the command line, add files and folders, set up the project path, define project shortcuts and create a reference to the new project in another project.

Set up the Example Files

1. Create a working copy of the Airframe Example project and open the project. MATLAB® copies the files to an examples folder so that you can edit them. Use currentProject to create a project object from the currently loaded project.

sldemo_slproject_airframe;
Starting: Simulink
Building with 'MinGW64 Compiler (C)'.
MEX completed successfully.
mainProject = currentProject
mainProject = 
  Project with properties:

                        Name: "Airframe Example"
    SourceControlIntegration: "Git"
          RepositoryLocation: "C:\workSpace\examples\repositories\airframe"
       SourceControlMessages: ["Branch status: Normal"    "No remote tracking branch"    "Current branch: master"]
                    ReadOnly: 0
                    TopLevel: 1
                Dependencies: [1×1 digraph]
                  Categories: [1×1 matlab.project.Category]
                       Files: [1×31 matlab.project.ProjectFile]
                   Shortcuts: [1×7 matlab.project.Shortcut]
                 ProjectPath: [1×7 matlab.project.PathFolder]
           ProjectReferences: [1×0 matlab.project.ProjectReference]
                StartupFiles: [1×0 string]
               ShutdownFiles: [1×0 string]
                 Description: "This is an example project.↵↵Use the "Project Shortcuts" toolstrip tab to find ways of getting started with this project."
                  RootFolder: "C:\workSpace\examples\airframe"
         SimulinkCacheFolder: "C:\workSpace\examples\airframe\work\cache"
        ProjectStartupFolder: "C:\workSpace\examples\airframe"
       SimulinkCodeGenFolder: "C:\workSpace\examples\airframe\work\codegen"

The Airframe Example project is a top level project (TopLevel: 1) with no referenced projects (ProjectReferences: [1x0]).

Create New Project

2. Create a new project called Wind Gust Library. Airframe project will use Wind Gust Library through a project reference.

a. Create a blank project and set the project name.

windGustFolder = fullfile(mainProject.RootFolder, "refs", "Wind Gust Library");
windGust = matlab.project.createProject(windGustFolder);
windGust.Name = "Wind Gust Library";

b. Move the wind_gust_lib.slx from the main project to the new folder, and add it to the Wind Gust Library project. Then, remove the file from the Airframe Example project.

movefile("..\..\models\wind_gust_lib.slx");
addFile(windGust, "wind_gust_lib.slx");

reload(mainProject);
removeFile(mainProject,"models\wind_gust_lib.slx");

c. Add the Wind Gust Library project root folder to the Wind Gust Library project path. This makes the files available when the Airframe Example project or any project that references the Wind Gust Library project is loaded.

reload(windGust);
addPath(windGust, windGust.RootFolder);

d. Create a Wind Gust Library project shortcut.

shortcut = addShortcut(windGust, "wind_gust_lib.slx");
shortcut.Group = "Top Level Model";

Add a Project Reference

3. Add the new Wind Gust Library project to the Airframe Example project as a project reference. This allows the Airframe Example project to view, edit, and run files in the Wind Gust Library project.

reload(mainProject);
addReference(mainProject, windGust)
ans = 
  ProjectReference with properties:

           Project: [1×1 matlab.project.Project]
              File: "C:\workSpace\examples\airframe\refs\Wind Gust Library"
    StoredLocation: "refs/Wind Gust Library"
              Type: "Relative"

The main project Airframe Example references the Wind Gust Library stored in "../refs/Wind Gust Library".

4. Use ProjectReferences method to query the Wind Gust Library project.

mainProject.ProjectReferences(1).Project
ans = 
  Project with properties:

                        Name: "Wind Gust Library"
    SourceControlIntegration: ""
          RepositoryLocation: ""
       SourceControlMessages: [1×0 string]
                    ReadOnly: 1
                    TopLevel: 0
                Dependencies: [1×1 digraph]
                  Categories: [1×1 matlab.project.Category]
                       Files: [1×1 matlab.project.ProjectFile]
                   Shortcuts: [1×1 matlab.project.Shortcut]
                 ProjectPath: [1×1 matlab.project.PathFolder]
           ProjectReferences: [1×0 matlab.project.ProjectReference]
                StartupFiles: [1×0 string]
               ShutdownFiles: [1×0 string]
                 Description: ""
                  RootFolder: "C:\workSpace\examples\airframe\refs\Wind Gust Library"

The Wind Gust Library project is not a top level project (TopLevel: 0). It is referenced by the the top level project Airframe Example (TopLevel: 1).

Further Information

Componentization of Large Projects