Main Content

プログラムによるプロジェクトの作成と参照

この例では、プログラムで新しいプロジェクトを作成し、それを参照プロジェクトとしてメインのプロジェクトに追加する方法を説明します。コマンド ラインからのプロジェクトの作成、ファイルとフォルダーの追加、プロジェクト パスの設定、プロジェクトのショートカットの定義、および別プロジェクトの新しいプロジェクトへの参照の作成方法を説明します。

例のファイルの設定

1. 機体のプロジェクトを開きます。currentProject を使用して、現在読み込まれているプロジェクトからプロジェクト オブジェクトを作成します。

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

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

Airframe Example プロジェクトは、参照プロジェクト (ProjectReferences: [1x1]). を 1 つもつ最上位レベル プロジェクト (TopLevel: 1) です。

新規プロジェクトの作成

2. Wind Gust Library と呼ばれる新規プロジェクトを作成します。Airframe プロジェクトはプロジェクト参照を通じて Wind Gust Library を使用します。

a. 空のプロジェクトを作成してプロジェクト名を設定します。

windGustFolder = fullfile(mainProject.RootFolder,"..","WindGustLibrary");
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: [1x1 matlab.project.Project]
              File: "/tmp/Bdoc24a_2511836_2327779/tp3129e2a7/simulink/AirframeProjectWithOneReferencedProjectExample/WindGustLibrary"
    StoredLocation: "../WindGustLibrary"
              Type: "Relative"

メインのプロジェクト Airframe Example"../refs/Wind Gust Library" に格納された Wind Gust Library を参照します。

4. ProjectReferences メソッドを使用して Wind Gust Library プロジェクトをクエリします。

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

                        Name: "Wind Gust Library"
                 Description: ""
                  RootFolder: "/tmp/Bdoc24a_2511836_2327779/tp3129e2a7/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]

Wind Gust Library プロジェクトは最上位プロジェクト (TopLevel: 0) ではありません。これは最上位プロジェクト Airframe Example (TopLevel: 1) によって参照されます。

プロジェクトを閉じる

5. プロジェクトを閉じてシャットダウン スクリプトを実行し、未保存のファイルを確認します。

close(mainProject)

参考

大規模なプロジェクトのコンポーネント化