Main Content

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

スクリプトを使用したプロジェクト タスクの自動化

この例では、プロジェクト API を使用して、変更済みファイル、依存関係、ショートカットおよびラベルの操作も含めて、ファイルを操作するプロジェクト タスクを自動化する方法を示します。

プロジェクトを開く

機体のサンプル プロジェクトを開きます。

openExample("simulink/AirframeProjectExample")
Building with 'gcc'.
MEX completed successfully.

コマンド ラインでのプロジェクトの取得

currentProject を使用してコマンド ラインでプロジェクトを操作するためのプロジェクト オブジェクトを取得します。

proj = currentProject
proj = 

  Project with properties:

                        Name: "Airframe Example"
                 Description: "This is an example project...."
                  RootFolder: "/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/AirframeProjectExample/airframe"
                    TopLevel: 1
                    ReadOnly: 0
         DefinitionFilesType: FixedPathMultiFile
    SourceControlIntegration: "Git"
          RepositoryLocation: "/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/repositories/AirframeProjectExample"
       SourceControlMessages: ["Branch status: Normal"    ...    ] (1x3 string)
                       Files: [1x30 matlab.project.ProjectFile]
                   Shortcuts: [1x3 matlab.project.Shortcut]
                  Categories: [1x1 matlab.project.Category]
                Dependencies: [1x1 digraph]
                StartupFiles: [1x0 string]
               ShutdownFiles: [1x0 string]
                 ProjectPath: [1x7 matlab.project.PathFolder]
           ProjectReferences: [1x0 matlab.project.ProjectReference]
        ProjectStartupFolder: "/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/AirframeProjectExample/airframe"
         SimulinkCacheFolder: "/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/AirframeProjectExample/airframe/work/cache"
       SimulinkCodeGenFolder: "/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/AirframeProjectExample/airframe/work/codegen"
         DependencyCacheFile: ""

プロジェクトのコマンドの検索

プロジェクトで何ができるか調べます。

methods(proj)
Methods for class matlab.project.Project:

addFile                       listModifiedFiles             
addFolderIncludingChildFiles  listRequiredFiles             
addPath                       refreshSourceControl          
addReference                  reload                        
addShortcut                   removeCategory                
addShutdownFile               removeFile                    
addStartupFile                removePath                    
close                         removeReference               
createCategory                removeShortcut                
export                        removeShutdownFile            
findCategory                  removeStartupFile             
findFile                      runChecks                     
isLoaded                      updateDependencies            
listAllProjectReferences      
listImpactedFiles             

Call "methods('handle')" for methods of matlab.project.Project inherited from handle.

プロジェクト ファイルの確認

プロジェクト オブジェクトを取得後、ファイルなどのプロジェクト プロパティを確認できます。

files = proj.Files
files = 

  1x30 ProjectFile array with properties:

    Path
    Revision
    SourceControlStatus
    Labels

このリストにあるファイルにアクセスするためのインデックスを作成します。次のコマンドでは、ファイル番号 10 のファイルが取得されます。各ファイルには、ファイルのパスおよびファイルに付けられたラベルを示すプロパティがあります。

proj.Files(10)
ans = 

  ProjectFile with properties:

                   Path: "/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/AirframeProjectExample/airframe/lib"
               Revision: "5996bce89517029faa0d0c4314cc0e1801dfc754"
    SourceControlStatus: NotUnderSourceControl
                 Labels: [1x0 matlab.project.Label]

10 番目のファイルのラベルを調べます。

proj.Files(10).Labels
ans = 

  1x0 Label array with properties:

    File
    DataType
    Data
    Name
    CategoryName

名前を指定して、特定のファイルを取得します。

myfile = findFile(proj, 'models/AnalogControl.slx')
myfile = 

  ProjectFile with properties:

                   Path: "/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/AirframeProjectExample/airframe/models/AnalogControl.slx"
               Revision: "5996bce89517029faa0d0c4314cc0e1801dfc754"
    SourceControlStatus: Unmodified
                 Labels: [1x1 matlab.project.Label]

ファイルで何ができるか調べます。

methods(myfile)
Methods for class matlab.project.ProjectFile:

addLabel     findLabel    removeLabel  

Call "methods('handle')" for methods of matlab.project.ProjectFile inherited from handle.

変更済みファイルの取得

任意のブロックを追加してプロジェクト モデル ファイルを変更します。

load_system('AnalogControl')
add_block('built-in/SubSystem', 'AnalogControl/test')
save_system('AnalogControl')

プロジェクトの変更済みファイルをすべて取得します。2 つの変更済みファイルを確認します。変更済みモデル ファイルが表示されるプロジェクトの [変更済みファイル] ビューおよび対応するプロジェクト定義ファイルと比較します。

modifiedfiles = listModifiedFiles(proj)
modifiedfiles = 

  1x2 ProjectFile array with properties:

    Path
    Revision
    SourceControlStatus
    Labels

2 番目の変更済みファイルを取得します。ファイルの SourceControlStatus プロパティが [変更済み] であることを確認します。同様に、listModifiedFiles は、プロジェクトの [変更済みファイル] ビューに表示される、追加、競合、削除されたファイルをすべて返します。

modifiedfiles(2)
ans = 

  ProjectFile with properties:

                   Path: "/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/AirframeProjectExample/airframe/resources/project/uuid-8d07b155-fd69-4712-9c50-4ee02cf3fb58.xml"
               Revision: ""
    SourceControlStatus: Added

個々のファイルでクエリを実行する前にソース管理ステータスを更新します。listModifiedFiles を使用する前に行う必要はありません。

refreshSourceControl(proj)

特定のソース管理ステータスをもつすべてのプロジェクト ファイルを取得します。たとえば、Unmodified のファイルを取得します。

proj.Files(ismember([proj.Files.SourceControlStatus], matlab.sourcecontrol.Status.Unmodified))
ans = 

  1x21 ProjectFile array with properties:

    Path
    Revision
    SourceControlStatus
    Labels

ファイルの依存関係の取得

ファイルの依存関係を更新します。プロジェクトは依存関係の分析を実行し、プロジェクト ファイル間の既知の依存関係を更新します。

updateDependencies(proj)

機体のプロジェクトで依存関係のリストを取得します。Dependencies プロパティには、MATLAB digraph オブジェクトとして保存されている、プロジェクト ファイル間の依存関係のグラフが含まれます。

g = proj.Dependencies
g = 

  digraph with properties:

    Edges: [21x1 table]
    Nodes: [21x1 table]

モデルで必要なファイルを取得します。

requiredFiles = bfsearch(g, which('AnalogControl'))
requiredFiles =

  3x1 cell array

    {'/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/AirframeProjectExample/airframe/models/AnalogControl.slx'}
    {'/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/AirframeProjectExample/airframe/data/controller.sldd'    }
    {'/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/AirframeProjectExample/airframe/data/buses.sldd'         }

グラフ内のすべてのタイプの最上位ファイルを取得します。

g.Nodes.Name(indegree(g)==0);

依存関係のある最上位ファイルを取得します。

g.Nodes.Name(indegree(g)==0 & outdegree(g)>0)
ans =

  4x1 cell array

    {'/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/AirframeProjectExample/airframe/models/DigitalControl.slx'}
    {'/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/AirframeProjectExample/airframe/models/LinearActuator.slx'}
    {'/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/AirframeProjectExample/airframe/models/slproject_f14.slx' }
    {'/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/AirframeProjectExample/airframe/tests/f14_airframe_test.m'}

転置されたグラフを作成し、影響を受けた (または "上流の") ファイルを見つけます。

transposed   = flipedge(g)
impacted = bfsearch(transposed, which('vertical_channel'))
transposed = 

  digraph with properties:

    Edges: [21x1 table]
    Nodes: [21x1 table]


impacted =

  4x1 cell array

    {'/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/AirframeProjectExample/airframe/models/vertical_channel.slx'}
    {'/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/AirframeProjectExample/airframe/models/f14_airframe.slx'    }
    {'/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/AirframeProjectExample/airframe/models/slproject_f14.slx'   }
    {'/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/AirframeProjectExample/airframe/tests/f14_airframe_test.m'  }

データ ディクショナリの影響を受けたファイルを見つけます。

impacted2 = bfsearch(transposed, which('buses.sldd'))
impacted2 =

  11x1 cell array

    {'/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/AirframeProjectExample/airframe/data/buses.sldd'             }
    {'/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/AirframeProjectExample/airframe/data/controller.sldd'        }
    {'/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/AirframeProjectExample/airframe/data/system_model.sldd'      }
    {'/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/AirframeProjectExample/airframe/tests/f14_airframe_test.m'   }
    {'/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/AirframeProjectExample/airframe/models/AnalogControl.slx'    }
    {'/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/AirframeProjectExample/airframe/models/DigitalControl.slx'   }
    {'/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/AirframeProjectExample/airframe/models/LinearActuator.slx'   }
    {'/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/AirframeProjectExample/airframe/models/NonLinearActuator.slx'}
    {'/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/AirframeProjectExample/airframe/models/f14_airframe.slx'     }
    {'/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/AirframeProjectExample/airframe/models/slproject_f14.slx'    }
    {'/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/AirframeProjectExample/airframe/models/vertical_channel.slx' }

依存関係や孤立したファイルの数など、ファイルの情報を取得します。

averageNumDependencies = mean(outdegree(g));
numberOfOrphans = sum(indegree(g)+outdegree(g)==0);

モデルの階層構造に変更を加える場合は、ボトムアップ方式で開始し、トポロジカル順序で検索します。

ordered = g.Nodes.Name(flip(toposort(g)));

ショートカットのクエリ

プロジェクトの [ショートカット] プロパティを調べます。

shortcuts = proj.Shortcuts
shortcuts = 

  1x3 Shortcut array with properties:

    Name
    Group
    File

配列内のショートカットを調べます。

shortcuts(3)
ans = 

  Shortcut with properties:

     Name: "Rebuild Project's S-functions"
    Group: "Utility"
     File: "/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/AirframeProjectExample/airframe/utilities/rebuild_s_functions.m"

ショートカットのファイル パスを取得します。

shortcuts(3).File
ans = 

    "/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/AirframeProjectExample/airframe/utilities/rebuild_s_functions.m"

ショートカット cell 配列内のすべてのファイルを調べます。

{shortcuts.File}'
ans =

  3x1 cell array

    {["/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/AirframeProjectExample/airframe/models/slproject_f14.slx"       ]}
    {["/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/AirframeProjectExample/airframe/reports/slproject_f14.pdf"      ]}
    {["/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/AirframeProjectExample/airframe/utilities/rebuild_s_functions.m"]}

ファイルのラベル

char 型の、ラベルの新しいカテゴリを作成します。プロジェクトで、新しい Engineers カテゴリが [ラベル] ペインに表示されます。

createCategory(proj, 'Engineers', 'char')
ans = 

  Category with properties:

                Name: "Engineers"
        SingleValued: 0
            DataType: "char"
    LabelDefinitions: [1x0 matlab.project.LabelDefinition]

新規カテゴリで何ができるか調べます。

category = findCategory(proj, 'Engineers');
methods(category)
Methods for class matlab.project.Category:

createLabel  findLabel    removeLabel  

Call "methods('handle')" for methods of matlab.project.Category inherited from handle.

新しいカテゴリに新しいラベルを定義します。

createLabel(category, 'Bob');

ラベル定義を取得します。

ld = findLabel(category, 'Bob')
ld = 

  LabelDefinition with properties:

            Name: "Bob"
    CategoryName: "Engineers"

取得したファイルにラベル myfile を付けます。ファイルをプロジェクトで選択すると、ラベル エディター ペインでこのラベルを確認できます。

addLabel(myfile, 'Engineers', 'Bob');

特定のラベルを取得して、テキストなどのデータをそのラベルに付加します。

label = findLabel(myfile, 'Engineers', 'Bob');
label.Data = 'Please assess'
label = 

  Label with properties:

            File: "/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/AirframeProjectExample/airframe/models/AnalogControl.slx"
        DataType: "char"
            Data: 'Please assess'
            Name: "Bob"
    CategoryName: "Engineers"

次のように、ラベル データの変数を指定できます。

mydata = label.Data
mydata =

    'Please assess'

数値データ型の新しいラベル カテゴリを作成します。

createCategory(proj, 'Assessors', 'double');
category = findCategory(proj, 'Assessors');
createLabel(category, 'Sam');

指定したファイルに新しいラベルを付加し、データ値 2 をそのラベルに割り当てます。

myfile = proj.Files(14);
addLabel(myfile, 'Assessors', 'Sam', 2)
ans = 

  Label with properties:

            File: "/tmp/Bdoc23b_2355056_3214474/tp23b579d4/simulink/AirframeProjectExample/airframe/models/AnalogControl.slx"
        DataType: "double"
            Data: 2
            Name: "Sam"
    CategoryName: "Assessors"

プロジェクトを閉じる

コマンド ラインでプロジェクトを閉じる処理は、プロジェクト ツールを使用してプロジェクトを閉じる場合と同じです。たとえば、プロジェクトはシャットダウン スクリプトを実行し、未保存のモデルを確認します。

close(proj)

詳細

API の使用方法の詳細については、doc currentProject と入力してください。

起動タスクと終了タスクの自動化の詳細については、起動タスクの自動化を参照してください。

参考

| | | |

関連するトピック