В этом примере показано, как программно создать новый проект и добавить его как ссылочный проект в вашем основном проекте. Это покрывает, как создать проект из командной строки, добавьте файлы и папки, создайте путь к проекту, задайте ярлыки проекта и создайте ссылку на новый проект в другом проекте.
1. Создайте рабочую копию Airframe Example проект и открытый проект. MATLAB® копирует файлы в папку в качестве примера так, чтобы можно было отредактировать их. Используйте currentProject чтобы создать проект возражают из в настоящее время загруженного проекта.
sldemo_slproject_airframe_api;
Starting: Simulink
mainProject = currentProject
mainProject =
Project with properties:
Name: "Airframe Example"
SourceControlIntegration: "Git"
RepositoryLocation: "C:\workSpace\examples\repositories\airRef8"
SourceControlMessages: ["No remote tracking branch" "Current branch: master" "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]
Description: "This example project demonstrates the Project referencing feature."
RootFolder: "C:\workSpace\examples\airRef8\main"
SimulinkCodeGenFolder: "C:\workSpace\examples\airRef8\main\work\codegen"
SimulinkCacheFolder: "C:\workSpace\examples\airRef8\main\work\cache"
ProjectStartupFolder: "C:\workSpace\examples\airRef8\main"
The Airframe Example проект является проектом верхнего уровня (TopLevel: 1) с одним ссылочным проектом (ProjectReferences: [1x1]).

2. Создайте новый проект под названием Wind Gust Library. Airframe проект будет использовать Wind Gust Library через ссылку проекта.
a. Создайте пустой проект и установите название проекта.
windGustFolder = fullfile(mainProject.RootFolder, "..", "refs", "Wind Gust Library"); windGust = matlab.project.createProject(windGustFolder); windGust.Name = "Wind Gust Library";
b. Добавьте data папка и wind_gust_lib.slx файл к Wind Gust Library проект.
addFolderIncludingChildFiles(windGust, "data"); addFile(windGust, "wind_gust_lib.slx");
c. Добавьте data папка и Wind Gust Library корневая папка проекта к Wind Gust Library путь к проекту. Это делает файлы доступными когда Airframe Example проект или любой проект это ссылается на Wind Gust Library проект загружается.
addPath(windGust, "data");
addPath(windGust, windGust.RootFolder); d. Создайте Wind Gust Library ярлык проекта.
shortcut = addShortcut(windGust, "wind_gust_lib.slx"); shortcut.Group = "Top Level Model";
3. Добавьте новый Wind Gust Library проект к Airframe Example проект как ссылка проекта. Это позволяет Airframe Example проект просмотреть, отредактируйте и петляйте в Wind Gust Library проект.
reload(mainProject); addReference(mainProject, windGust)
ans =
ProjectReference with properties:
Project: [1×1 matlab.project.Project]
File: "C:\workSpace\examples\airRef8\refs\Wind Gust Library"
StoredLocation: "../refs/Wind Gust Library"
Type: "Relative"
Основной проект Airframe Example ссылается на Wind Gust Library сохраненный в "../refs/Wind Gust Library".

4. Используйте ProjectReferences метод, чтобы запросить 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]
Description: ""
RootFolder: "C:\workSpace\examples\airRef8\refs\Wind Gust Library"
Wind Gust Library проект не является проектом верхнего уровня (TopLevel: 0). На это ссылается проект верхнего уровня Airframe Example (TopLevel: 1).