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

1. 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/Bdoc24a_2528353_1901164/tp667c3d2d/simulink/AirframeProjectWithOneReferencedProjectExample/airRef"
                    TopLevel: 1
                    ReadOnly: 0
         DefinitionFilesType: FixedPathMultiFile
    SourceControlIntegration: ""
          RepositoryLocation: ""
       SourceControlMessages: [1x0 string]
                       Files: [1x22 matlab.project.ProjectFile]
                   Shortcuts: [1x2 matlab.project.Shortcut]
                  Categories: [1x1 matlab.project.Category]
                Dependencies: [1x1 digraph]
                StartupFiles: [1x0 string]
               ShutdownFiles: [1x0 string]
                 ProjectPath: [1x4 matlab.project.PathFolder]
           ProjectReferences: [1x1 matlab.project.ProjectReference]
        ProjectStartupFolder: "/tmp/Bdoc24a_2528353_1901164/tp667c3d2d/simulink/AirframeProjectWithOneReferencedProjectExample/airRef"
         DependencyCacheFile: ""
         SimulinkCacheFolder: "/tmp/Bdoc24a_2528353_1901164/tp667c3d2d/simulink/AirframeProjectWithOneReferencedProjectExample/airRef/work/cache"
       SimulinkCodeGenFolder: "/tmp/Bdoc24a_2528353_1901164/tp667c3d2d/simulink/AirframeProjectWithOneReferencedProjectExample/airRef/work/codegen"

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

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,"..","WindGustLibrary");
windGust = matlab.project.createProject(windGustFolder);
windGust.Name = "Wind Gust Library";

b. 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");

c. 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);

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: [1x1 matlab.project.Project]
              File: "/tmp/Bdoc24a_2528353_1901164/tp667c3d2d/simulink/AirframeProjectWithOneReferencedProjectExample/WindGustLibrary"
    StoredLocation: "../WindGustLibrary"
              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(2).Project
ans = 
  Project with properties:

                        Name: "Wind Gust Library"
                 Description: ""
                  RootFolder: "/tmp/Bdoc24a_2528353_1901164/tp667c3d2d/simulink/AirframeProjectWithOneReferencedProjectExample/WindGustLibrary"
                    TopLevel: 0
                    ReadOnly: 1
         DefinitionFilesType: FixedPathMultiFile
    SourceControlIntegration: ""
          RepositoryLocation: ""
       SourceControlMessages: [1x0 string]
                       Files: [1x3 matlab.project.ProjectFile]
                   Shortcuts: [1x1 matlab.project.Shortcut]
                  Categories: [1x1 matlab.project.Category]
                Dependencies: [1x1 digraph]
                StartupFiles: [1x0 string]
               ShutdownFiles: [1x0 string]
                 ProjectPath: [1x2 matlab.project.PathFolder]
           ProjectReferences: [1x0 matlab.project.ProjectReference]

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

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

close(mainProject)

See Also

Componentization of Large Projects