Main Content

Create and Reference a Project Programmatically

This example shows how to programmatically 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

Open the Airframe project. Use currentProject to create a project object from the currently loaded project.

openExample("simulink/AirframeProjectWithOneReferencedProjectExample")
mainProject = currentProject
mainProject = 
  Project with properties:

                        Name: "Airframe Example"
                 Description: "This example project demonstrates the Project referencing feature."
                  RootFolder: "/tmp/Bdoc25a_2864802_2873889/tpab47e444/simulink/AirframeProjectWithOneReferencedProjectExample/airRef"
                    TopLevel: 1
                    ReadOnly: 0
         DefinitionFilesType: FixedPathMultiFile
    SourceControlIntegration: ""
          RepositoryLocation: ""
                       Files: [1×22 matlab.project.ProjectFile]
                   Shortcuts: [1×2 matlab.project.Shortcut]
                  Categories: [1×1 matlab.project.Category]
                Dependencies: [1×1 digraph]
                StartupFiles: [1×0 string]
               ShutdownFiles: [1×0 string]
                 ProjectPath: [1×4 matlab.project.PathFolder]
           ProjectReferences: [1×1 matlab.project.ProjectReference]
        ProjectStartupFolder: "/tmp/Bdoc25a_2864802_2873889/tpab47e444/simulink/AirframeProjectWithOneReferencedProjectExample/airRef"
         SimulinkCacheFolder: "/tmp/Bdoc25a_2864802_2873889/tpab47e444/simulink/AirframeProjectWithOneReferencedProjectExample/airRef/work/cache"
       SimulinkCodeGenFolder: "/tmp/Bdoc25a_2864802_2873889/tpab47e444/simulink/AirframeProjectWithOneReferencedProjectExample/airRef/work/codegen"
         DependencyCacheFile: ""

The Airframe Example project is a top level project (TopLevel: 1) with one referenced project (ProjectReferences: [1x1]).

Project panel with the References tab listing one referenced project

Create New Project

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

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

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

2. Add the data folder and the wind_gust_lib.slx file to the Wind Gust Library project.

addFolderIncludingChildFiles(windGust,"data");
addFile(windGust,"wind_gust_lib.slx");

3. Add the data folder and 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.

addPath(windGust,"data");
addPath(windGust,windGust.RootFolder);

4. Create a Wind Gust Library project shortcut.

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

Add a Project Reference

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: "/tmp/Bdoc25a_2864802_2873889/tpab47e444/simulink/AirframeProjectWithOneReferencedProjectExample/WindGustLibrary"
    StoredLocation: "../WindGustLibrary"
              Type: "Relative"

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

Project panel with the References tab listing two referenced projects

Use ProjectReferences method to query the Wind Gust Library project.

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

                        Name: "Wind Gust Library"
                 Description: ""
                  RootFolder: "/tmp/Bdoc25a_2864802_2873889/tpab47e444/simulink/AirframeProjectWithOneReferencedProjectExample/WindGustLibrary"
                    TopLevel: 0
                    ReadOnly: 1
         DefinitionFilesType: FixedPathMultiFile
    SourceControlIntegration: ""
          RepositoryLocation: ""
                       Files: [1×3 matlab.project.ProjectFile]
                   Shortcuts: [1×1 matlab.project.Shortcut]
                  Categories: [1×1 matlab.project.Category]
                Dependencies: [1×1 digraph]
                StartupFiles: [1×0 string]
               ShutdownFiles: [1×0 string]
                 ProjectPath: [1×2 matlab.project.PathFolder]
           ProjectReferences: [1×0 matlab.project.ProjectReference]
        ProjectStartupFolder: "/tmp/Bdoc25a_2864802_2873889/tpab47e444/simulink/AirframeProjectWithOneReferencedProjectExample/WindGustLibrary"
         SimulinkCacheFolder: ""
       SimulinkCodeGenFolder: ""
         DependencyCacheFile: ""

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

Close Project

Close the project to run shutdown scripts and check for unsaved files.

close(mainProject)

See Also

Componentize Large Projects