Manage Block Data Sets with JSON Files
This example shows how to update block parameter data in a JSON file. Simscape uses JSON files to store block parameter data and metadata. To update a block data set contained in a JSON file:
Import the data set that you want to update.
Update the parameter data by applying it to a block, changing the parameters in the block dialog box or using
set_param
, and getting the new block data set.When you are satisfied with your updates, export the block data set to the original JSON file.
Import a Block Data Set from a JSON File
The ExampleDataSet.json
file contains a block data set for an AC Voltage Source block. Use the partrepo.importDataSetsFromJSON
function to import the block data set for the AC Voltage Source block. Import the data set from the JSON file called ExampleDataSet
.
JSONdataSets = partrepo.importDataSetsFromJSON('ExampleDataSet');
myDataSet = JSONdataSets{1}
myDataSet = BlockDataSet with properties: ID: "Example Manufacturer|P000001|Example AC Voltage Source Parameterization" (format) Metadata: [1×1 Metadata] (11 entries) Parameters: [1×1 Parameters] (3 entries)
Apply the Data Set Parameter Values to a Simscape Block
Open the BandLimitedOpAmp
model. The model contains an AC Voltage Source block with Phase shift set to 0 deg
.
Use the model to generate a baseline voltage plot.
sim('BandLimitedOpAmp') open_system('BandLimitedOpAmp/Voltage')
The plot begins at 0.4 V
.
Use the applyParams
function to apply the block data set to the AC Voltage Source block.
blockPath = 'BandLimitedOpAmp/AC Voltage 1KHz/AC Voltage Source';
applyParams(myDataSet,blockPath)
The block now uses a slower frequency of 500 Hz
, compared to the 1000 Hz
baseline frequency. The wavelength is double the baseline results.
sim('BandLimitedOpAmp') open_system('BandLimitedOpAmp/Voltage')
Shift the phase of the AC Voltage Source block by 90 deg
.
set_param(blockPath,'shift','90') sim('BandLimitedOpAmp') open_system('BandLimitedOpAmp/Voltage')
The plot now begins near -0.8 V
.
Use the partrepo.simscape.paramsFromBlock
function to update the block data set to match the parameters in the AC Voltage Source block.
myDataSet.Parameters = partrepo.simscape.paramsFromBlock(blockPath)
myDataSet = BlockDataSet with properties: ID: "Example Manufacturer|P000001|Example AC Voltage Source Parameterization" (format) Metadata: [1×1 Metadata] (11 entries) Parameters: [1×1 Parameters] (3 entries)
Export the Block Data Set to JSON
Use the validate
function to validate the block data set.
validate(myDataSet)
Use the partrepo.exportDataSetsToJSON
function to save the data set to a new JSON file. Open the JSON data to verify that it contains the expected changes.
partrepo.exportDataSetsToJSON(myDataSet,'MyFirstDataSet') jsonData = fileread('MyFirstDataSet.json'); disp(jsonData)
{ "Metadata": [ { "Name": "Manufacturer", "Value": "Example Manufacturer" }, { "Name": "PartNumber", "Value": "P000001" }, { "Name": "BlockType", "Value": "SimscapeBlock" }, { "Name": "ParameterizationDate", "Value": "18-Nov-2024" }, { "Name": "SimulinkRelease", "Value": "25.1" }, { "Name": "ReferenceBlock", "Value": "fl_lib/Electrical/Electrical Sources/AC Voltage Source" }, { "Name": "LibraryVersion", "Value": "25001000.1" }, { "Name": "ModelingEntityId", "Value": "foundation.electrical.sources.ac_voltage" }, { "Name": "ModelingEntityType", "Value": "SimscapeComponent" }, { "Name": "CustomIdentifier", "Value": "Example AC Voltage Source Parameterization" }, { "Name": "PartSchemaId", "Value": "defaultSimscapeSchema" } ], "Parameters": [ { "Name": "amp", "ValueExpr": "1e-5", "Unit": "V", "Origin": "" }, { "Name": "frequency", "ValueExpr": "500", "Unit": "Hz", "Origin": "" }, { "Name": "shift", "ValueExpr": "90", "Unit": "deg", "Origin": "" } ] }
See Also
partrepo.simscape.BlockDataSet
| partrepo.importDataSetsFromJSON
| partrepo.exportDataSetsToJSON