最新のリリースでは、このページがまだ翻訳されていません。 このページの最新版は英語でご覧になれます。
この例では、プロジェクト API を使用して新規プロジェクトを作成し、ファイルを操作するプロジェクト タスクを自動化する方法を説明します。コマンド ラインからのプロジェクトの作成、ファイルとフォルダーの追加、プロジェクト パスの設定、プロジェクトのショートカットの定義、および別プロジェクトの新しいプロジェクトへの参照の作成方法を説明します。また、変更されたファイル、依存関係、ショートカット、およびラベルをプログラムにより操作する方法も示します。
サンプル プロジェクト Times Table App のファイルの作業用コピーを作成し、プロジェクトを開きます。MATLAB® により、ファイルが examples フォルダーにコピーされ、編集できるようになります。プロジェクトでは、ファイルが Git™ ソース管理下に置かれます。currentProject
を使用して、現在読み込まれているプロジェクトからプロジェクト オブジェクトを作成します。
matlab.project.example.timesTable mainProject = currentProject;
プロジェクト内のファイルを調べます。
files = mainProject.Files
files=1×14 object
1×14 ProjectFile array with properties:
Path
Labels
Revision
SourceControlStatus
このリストにあるファイルにアクセスするためのインデックスを使用します。たとえば、ファイル番号 10 を取得するとします。各ファイルには、ファイルのパスおよびファイルに付けられたラベルを示すプロパティがあります。
mainProject.Files(10)
ans = ProjectFile with properties: Path: "C:\workSpace\examples\TimesTableApp1\tests\tNewTimesTable.m" Labels: [1×1 matlab.project.Label] Revision: "51316c67b968d45a17e127003a25143577ec011a" SourceControlStatus: Unmodified
10 番目のファイルについて、Git の最新バージョンを取得します。
mainProject.Files(10).Revision
ans = "51316c67b968d45a17e127003a25143577ec011a"
10 番目のファイルのラベルを調べます。
mainProject.Files(10).Labels
ans = Label with properties: File: "C:\workSpace\examples\TimesTableApp1\tests\tNewTimesTable.m" DataType: 'none' Data: [] Name: "Test" CategoryName: "Classification"
名前を指定して、特定のファイルを取得します。
myfile = findFile(mainProject,"source/timestable.mlapp")
myfile = ProjectFile with properties: Path: "C:\workSpace\examples\TimesTableApp1\source\timestable.mlapp" Labels: [1×1 matlab.project.Label] Revision: "51316c67b968d45a17e127003a25143577ec011a" SourceControlStatus: Unmodified
プロジェクト Times Table Game を作成します。このプロジェクトは、Times Table App におけるゲーム ロジックを保存します。プロジェクト Times Table Game は、プロジェクト参照を通じてプロジェクト Times Table App で使用されます。
プロジェクトを作成し、プロジェクト名を設定します。
timesTableGameFolder = fullfile(mainProject.RootFolder,"refs","TimesTableGame"); timesTableGame = matlab.project.createProject(timesTableGameFolder); timesTableGame.Name = "Times Table Game";
ゲーム ロジック Times Table App をメインのプロジェクト フォルダーから新しいプロジェクト フォルダーに移動し、プロジェクト Times Table Game に追加します。次に、ファイルをプロジェクト Times Table App から削除します。
movefile("..\..\source\timesTableGame.m"); addFile(timesTableGame,"timesTableGame.m"); reload(mainProject); removeFile(mainProject,"source\timesTableGame.m");
プロジェクト Times Table Game のルート フォルダーをプロジェクト Times Table Game のパスに追加します。これによって、プロジェクト Times Table App またはプロジェクト Times Table App を参照するプロジェクトが読み込まれると、timesTableGame.m
ファイルが使用可能になります。
reload(timesTableGame); addPath(timesTableGame,timesTableGame.RootFolder);
新しいプロジェクト Times Table Game をプロジェクト参照としてプロジェクト Times Table App に追加します。これにより、プロジェクト Time Table App では、プロジェクト Times Table Game 内のファイルを表示、編集、および実行できるようになります。
reload(mainProject); addReference(mainProject,timesTableGame);
プロジェクト Times Table App で変更されたファイルをすべて取得します。このリストを、プロジェクト内の [ファイル]、[変更済み] ビューと比較します。新しいプロジェクト Times Table Game のファイルに加え、プロジェクト Times Table App で削除されたファイルと変更されたファイルを表示できます。
modifiedfiles = listModifiedFiles(mainProject)
modifiedfiles=1×8 object
1×8 ProjectFile array with properties:
Path
Labels
Revision
SourceControlStatus
リストの 2 番目の変更済みファイルを取得します。SourceControlStatus
プロパティが Added
であるか確認します。関数 listModifiedFiles
は、追加、変更、競合、削除などが行われたすべてのファイルを返します。
modifiedfiles(2)
ans = ProjectFile with properties: Path: "C:\workSpace\examples\TimesTableApp1\refs\TimesTableGame\resources\project\Project.xml" Revision: "" SourceControlStatus: Added
個々のファイルをクエリする前に、ソース管理のステータスを更新します。listModifiedFiles
を呼び出す前にこの作業を行う必要はありません。
refreshSourceControl(mainProject)
Unmodified
になっているすべてのプロジェクト ファイルを取得します。関数 ismember
を使用して、プロジェクト Times Table App で変更されていないファイルを示す logical 配列を取得します。この配列を使用して変更されていないファイルのリストを取得します。
unmodifiedStatus = ismember([mainProject.Files.SourceControlStatus],matlab.sourcecontrol.Status.Unmodified); mainProject.Files(unmodifiedStatus)
ans=1×9 object
1×9 ProjectFile array with properties:
Path
Labels
Revision
SourceControlStatus
依存関係の解析を実行して、プロジェクト ファイル間の既知の依関係を更新します。
updateDependencies(mainProject)
プロジェクト Times Table App の依存関係のリストを取得します。Dependencies
プロパティには、MATLAB digraph
プロジェクトとして保存されている、プロジェクト ファイル間の依存関係のグラフが含まれます。
g = mainProject.Dependencies
g = digraph with properties: Edges: [5×1 table] Nodes: [9×1 table]
timestable.mlapp
ファイルに必要なファイルを取得します。
requiredFiles = bfsearch(g, which('source/timestable.mlapp'))
requiredFiles = 2×1 cell
{'C:\workSpace\examples\TimesTableApp1\source\timestable.mlapp' }
{'C:\workSpace\examples\TimesTableApp1\refs\TimesTableGame\timesTableGame.m'}
グラフ内のすべてのタイプの最上位ファイルを取得します。関数 indegree
は、他のファイルと依存関係にないすべてのファイルを検索します。
top = g.Nodes.Name(indegree(g)==0)
top = 7×1 cell
{'C:\workSpace\examples\TimesTableApp1\requirements\TimesTableRequirements.mlx'}
{'C:\workSpace\examples\TimesTableApp1\tests\tAnswerIsCorrect.m' }
{'C:\workSpace\examples\TimesTableApp1\tests\tCurrentQuestion.m' }
{'C:\workSpace\examples\TimesTableApp1\tests\tNewTimesTable.m' }
{'C:\workSpace\examples\TimesTableApp1\utilities\editTimesTable.m' }
{'C:\workSpace\examples\TimesTableApp1\utilities\openRequirementsDocument.m' }
{'C:\workSpace\examples\TimesTableApp1\utilities\runTheseTests.m' }
依存関係のある最上位ファイルを取得します。関数 indegree
は、他のファイルと依存関係にないすべてのファイルを検索し、関数 outdegree
は依存関係があるすべてのファイルを検索します。
top = g.Nodes.Name(indegree(g)==0 & outdegree(g)>0)
top = 4×1 cell
{'C:\workSpace\examples\TimesTableApp1\requirements\TimesTableRequirements.mlx'}
{'C:\workSpace\examples\TimesTableApp1\tests\tAnswerIsCorrect.m' }
{'C:\workSpace\examples\TimesTableApp1\tests\tCurrentQuestion.m' }
{'C:\workSpace\examples\TimesTableApp1\tests\tNewTimesTable.m' }
転置されたグラフを作成し、影響を受けた (または "上流の") ファイルを見つけます。関数 flipedge
を使用して、グラフ内のエッジの方向を反転します。
transposed = flipedge(g)
transposed = digraph with properties: Edges: [5×1 table] Nodes: [9×1 table]
impacted = bfsearch(transposed,which('source/timestable.mlapp'))
impacted = 2×1 cell
{'C:\workSpace\examples\TimesTableApp1\source\timestable.mlapp' }
{'C:\workSpace\examples\TimesTableApp1\requirements\TimesTableRequirements.mlx'}
依存関係や孤立したファイルの数など、プロジェクト ファイルに関する情報を取得します。
averageNumDependencies = mean(outdegree(g)); numberOfOrphans = sum(indegree(g)+outdegree(g)==0);
依存関係グラフの並べ替え順序を変更して、プロジェクトの変更を最下位から順番に表示します。
ordered = g.Nodes.Name(flip(toposort(g)));
ショートカットを使用して、頻繁に実行するタスクや頻繁にアクセスするファイルを保存したり、起動タスクと終了タスクを自動化できます。
プロジェクト Times Table App のショートカットを取得します。
shortcuts = mainProject.Shortcuts
shortcuts=1×4 object
1×4 Shortcut array with properties:
Name
Group
File
リスト内のショートカットを調べます。
shortcuts(2)
ans = Shortcut with properties: Name: "Edit Times Table App" Group: "Launch Points" File: "C:\workSpace\examples\TimesTableApp1\utilities\editTimesTable.m"
ショートカットのファイル パスを取得します。
shortcuts(3).File
ans = "C:\workSpace\examples\TimesTableApp1\utilities\openRequirementsDocument.m"
ショートカット リスト内のすべてのファイルを調べます。
{shortcuts.File}'
ans=4×1 cell array
{["C:\workSpace\examples\TimesTableApp1\source\timestable.mlapp" ]}
{["C:\workSpace\examples\TimesTableApp1\utilities\editTimesTable.m" ]}
{["C:\workSpace\examples\TimesTableApp1\utilities\openRequirementsDocument.m"]}
{["C:\workSpace\examples\TimesTableApp1\utilities\runTheseTests.m" ]}
char
型のラベルの新しいカテゴリを作成します。プロジェクト Times Table App では、新しい Engineers
カテゴリが [ラベル] ペインに表示されます。
createCategory(mainProject,'Engineers','char')
ans = Category with properties: Name: "Engineers" SingleValued: 0 DataType: "char" LabelDefinitions: [1×0 matlab.project.LabelDefinition]
新しいカテゴリに新しいラベルを定義します。
category = findCategory(mainProject,'Engineers'); createLabel(category,'Bob');
新しいラベルのラベル定義オブジェクトを取得します。
ld = findLabel(category,'Bob')
ld = LabelDefinition with properties: Name: "Bob" CategoryName: "Engineers"
ラベルをプロジェクト ファイルに追加します。プロジェクト Times Table App でファイルを選択すると、"ラベル エディター" ペインにこのラベルが表示されます。
myfile = findFile(mainProject,"source/timestable.mlapp"); addLabel(myfile,'Engineers','Bob');
特定のラベルを取得し、テキスト データをそのラベルに付加します。
label = findLabel(myfile,'Engineers','Bob'); label.Data = 'Email: Bob.Smith@company.com'
label = Label with properties: File: "C:\workSpace\examples\TimesTableApp1\source\timestable.mlapp" DataType: 'char' Data: 'Email: Bob.Smith@company.com' Name: "Bob" CategoryName: "Engineers"
ラベル データを取得し、変数に保存します。
mydata = label.Data
mydata = 'Email: Bob.Smith@company.com'
double データ型の新しいラベル カテゴリを作成します。このデータ型は通常、MATLAB で数値データに使用されます。
createCategory(mainProject,'Assessors','double'); category = findCategory(mainProject,'Assessors'); createLabel(category,'Sam');
指定したファイルに新しいラベルを付加し、データ値 2 をそのラベルに割り当てます。
myfile = mainProject.Files(10); addLabel(myfile, 'Assessors', 'Sam', 2)
ans = Label with properties: File: "C:\workSpace\examples\TimesTableApp1\utilities" DataType: 'double' Data: 2 Name: "Sam" CategoryName: "Assessors"
プロジェクトを閉じて、シャットダウン スクリプトを実行し、未保存のファイルがないか確認します。
close(mainProject)