Search Programmatically
To take action programmatically in Simulink®, you must specify the target object you want to take the action on. The target object can be a model, a component— such as a subsystem or library— or a model element, such as a block or signal line. To specify the target object, use a handle or path. You can specify target objects whose names and locations you know, or you can programmatically search for target objects that fit certain criteria. This topic explains how to programmatically search for target objects. For information about handles, paths, and how to get them for target objects whose names and locations you know, see Get Handles and Paths.
Find Model Elements
To search for model elements that fit certain criteria, use these functions. See the
function documentation to select the syntax that outputs the search results in the data type
you want. For example, to get the handles of all Integrator
blocks in a
model with the handle hModel
, use one of the commands in the
Example column of the table.
Action | Function | Example |
---|---|---|
Find models, blocks, lines, ports, and annotations | find_system |
h = find_system(hModel,BlockType="Integrator") |
Find blocks | Simulink.findBlocks |
h = Simulink.findBlocks(hModel,BlockType="Integrator") |
Find blocks of a specific type | Simulink.findBlocksOfType |
h = Simulink.findBlocksOfType(hModel,"Integrator") |
The searches you can perform with the Simulink.findBlocks
and Simulink.findBlocksOfType
functions can also be performed with the find_system
function, but the Simulink.findBlocks
and Simulink.findBlocksOfType
functions are more straightforward to use and
troubleshoot.
To find model elements that connect to a model element you specify, such as to find the ports of a block, the signal lines that connect to a block, or the blocks that connect to a signal line, see Get Handle of Attached Block, Attached Ports, and Connected Signal Lines.
Find Models
To search for all loaded Simulink models, all loaded Simulink libraries, or all loaded Simulink block diagrams including models and libraries, use the Simulink.allBlockDiagrams
function.
To search for models that fit specific criteria, use the modelfinder
function to create a database of models and then search that database for the models. For
more information, see Index and Search Models with Model Finder.
To search for SLX files, MDL files, Model blocks, or
Subsystem blocks, use the find_mdlrefs
or the pathsToReferencedModel
function. The find_mdlrefs
function searches the SLX file, MDL file, Model block, or
Subsystem block you specify for all referenced models and
Model blocks. The pathsToReferencedModel
function
searches the model you specify for all referenced models and Model blocks,
but the search stops at the referenced model you specify. The function does not search the
contents of the specified reference model.
To search for models according to the content of their metadata and descriptions, use
the Simulink.MDLInfo
object or the get_param
function.
To set the model metadata, create a structure that holds the metadata. For example, to create a metadata structure that specifies the expected completion date of the model as tomorrow and the test status as untested, use these commands.
t=datetime('tomorrow','format','MM/dd/yy'); m.ExpectedCompletionDate = t; m.TestStatus = "untested";
Load the model and get the model handle.
h = load_system("myModel")
Use the handle and the set_param
function to set the value of the
Metadata
parameter.
set_param(h,"Metadata",m)
To get the metadata of a loaded model, use the get_param
function.
data = get_param(h,"Metadata")
To get metadata without loading a model, use the Simulink.MDLInfo.getMetadata
function.
To set the model description, use the set_param
function to set the
value of the Description
parameter.
set_param(myModel,"Description",... "Enter the description text here.")
To get the description of a loaded model, use the get_param
function.
desc = get_param(h,"Description")
To get a description without loading a model, use the Simulink.MDLInfo.getDescription
function.
To set or get the metadata or descriptions of multiple models at once using the
set_param
or get_param
functions, specify the
models as a vector of handles or as filenames in a string array or cell array of character
vectors. For example, these commands get all models whose descriptions contain the word
clutch
.
name = ["myModel1";"myModel2";"myModel3"]; desc = get_param(name,"Description"); clutchModels = name(contains(desc,"clutch",ignoreCase=true))
See Also
Simulink.allBlockDiagrams
| modelfinder
| Simulink.MDLInfo
| find_system
| Simulink.findBlocks
| Simulink.findBlocksOfType