主要内容

Create a Part Collection

This example shows how to create a part collection and add Simscape parts to the collection.

Create Part Collection

Create a new collection using partrepo.collection.new.

collectionName = "MyResistors";
orgID = "MyOrg";
folderName = "MyResistors";
if exist(folderName,"dir")
    rmdir(folderName, 's');
end
partrepo.collection.new(collectionName,orgID)

Simscape creates a folder called MyResistors that contains an empty folder called Datasets and a JSON file called CollectionInfo.json that contains information about the collection. Edit CollectionInfo.json to capture information about the collection. You can open the file and directly edit it. To edit the data programmatically, enter:

filePath = "MyResistors/CollectionInfo.json";
CollectionInfoStr = fileread(filePath);
data = jsondecode(CollectionInfoStr);
data.Version = "1.0.1";
data.Description = "My Resistor Collection";
CollectionInfoStr = jsonencode(data,PrettyPrint=true);
writelines(CollectionInfoStr,filePath,WriteMode="overwrite");
disp(fileread(filePath))
{
  "Name": "MyResistors",
  "OrganizationId": "MyOrg",
  "Version": "1.0.1",
  "Author": "",
  "Description": "My Resistor Collection"
}

Create Data Sets and Add Them to Collection

The MyResistorModel model contains three Simscape foundation library Resistor blocks in parallel. Open the model.

open("MyResistorModel")

Update the resistance of the block labeled Resistor 1.

blockName = "MyResistorModel/Resistor 1";
set_param(blockName,R="33");

Use partrepo.simscape.dataSetFromBlock to create a block data set from the Resistor 1 block.

dataSet = partrepo.simscape.dataSetFromBlock(blockName)
dataSet = 
  BlockDataSet with properties:

            ID: "||" (format)
      Metadata: [1×1 Metadata] (11 entries, Incomplete)
    Parameters: [1×1 Parameters] (1 entries)

To export the block data set, first add basic metadata. Then, validate the block data set using validate.

dataSet.Metadata.Manufacturer = "MyManufacturer";
dataSet.Metadata.PartNumber = "R-0001";
dataSet.Metadata.CustomIdentifer = '33 ohm resistor'
dataSet = 
  BlockDataSet with properties:

            ID: "MyManufacturer|R-0001|" (format)
      Metadata: [1×1 Metadata] (12 entries)
    Parameters: [1×1 Parameters] (1 entries)

validate(dataSet)

Export the data set to JSON and store it in the Datasets folder of the collection.

partrepo.exportDataSetsToJSON(dataSet,"MyResistors/Datasets/" ...
    + dataSet.Metadata.Manufacturer  ...
    + "_" + dataSet.Metadata.PartNumber)

Build Shareable Collection

To convert the JSON data to a shareable MLDATX collection, use partrepo.collection.build.

partrepo.collection.build("MyResistors/")

The generated MLDATX file is in the Build folder.

See Also

Topics