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")
Starting: Simulink
mainProject = currentProject
mainProject = 
  Project with properties:

                        Name: "Airframe Example"
                 Description: "This example project demonstrates the Project referencing feature."
                  RootFolder: "/tmp/Bdoc24b_2679053_1232944/tp1e2fc5f6/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]
       SimulinkCodeGenFolder: "/tmp/Bdoc24b_2679053_1232944/tp1e2fc5f6/simulink/AirframeProjectWithOneReferencedProjectExample/airRef/work/codegen"
        ProjectStartupFolder: "/tmp/Bdoc24b_2679053_1232944/tp1e2fc5f6/simulink/AirframeProjectWithOneReferencedProjectExample/airRef"
         DependencyCacheFile: ""
         SimulinkCacheFolder: "/tmp/Bdoc24b_2679053_1232944/tp1e2fc5f6/simulink/AirframeProjectWithOneReferencedProjectExample/airRef/work/cache"

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

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: [1x1 matlab.project.Project]
              File: "/tmp/Bdoc24b_2679053_1232944/tp1e2fc5f6/simulink/AirframeProjectWithOneReferencedProjectExample/WindGustLibrary"
    StoredLocation: "../WindGustLibrary"
              Type: "Relative"

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

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/Bdoc24b_2679053_1232944/tp1e2fc5f6/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

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

close(mainProject)

See Also

Componentization of Large Projects