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. 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_api;
mainProject = currentProject
mainProject = 
  Project with properties:

                        Name: "Airframe Example"
    SourceControlIntegration: "Git"
          RepositoryLocation: "C:\workSpace\examples\repositories\airRef9"
       SourceControlMessages: ["Current branch: master"    "No remote tracking branch"    "Branch status: Normal"]
                    ReadOnly: 0
                    TopLevel: 1
                Dependencies: [1×1 digraph]
                  Categories: [1×1 matlab.project.Category]
                       Files: [1×22 matlab.project.ProjectFile]
                   Shortcuts: [1×5 matlab.project.Shortcut]
                 ProjectPath: [1×4 matlab.project.PathFolder]
           ProjectReferences: [1×1 matlab.project.ProjectReference]
                StartupFiles: [1×0 string]
               ShutdownFiles: [1×0 string]
         DefinitionFilesType: FixedPathMultiFile
                 Description: "This example project demonstrates the Project referencing feature."
                  RootFolder: "C:\workSpace\examples\airRef9\airRef"
         SimulinkCacheFolder: "C:\workSpace\examples\airRef9\airRef\work\cache"
        ProjectStartupFolder: "C:\workSpace\examples\airRef9\airRef"
       SimulinkCodeGenFolder: "C:\workSpace\examples\airRef9\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: [1×1 matlab.project.Project]
              File: "C:\workSpace\examples\airRef9\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"
    SourceControlIntegration: ""
          RepositoryLocation: ""
       SourceControlMessages: [1×0 string]
                    ReadOnly: 1
                    TopLevel: 0
                Dependencies: [1×1 digraph]
                  Categories: [1×1 matlab.project.Category]
                       Files: [1×3 matlab.project.ProjectFile]
                   Shortcuts: [1×1 matlab.project.Shortcut]
                 ProjectPath: [1×2 matlab.project.PathFolder]
           ProjectReferences: [1×0 matlab.project.ProjectReference]
                StartupFiles: [1×0 string]
               ShutdownFiles: [1×0 string]
         DefinitionFilesType: FixedPathMultiFile
                 Description: ""
                  RootFolder: "C:\workSpace\examples\airRef9\WindGustLibrary"

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