Main Content

slreportgen.utils.sortObjects

Sort Simulink objects

Since R2022b

Description

sortedList = slreportgen.utils.sortObjects(objectList) sorts the elements in objectList alphabetically by name. You must load the models that contain the objects before using the sortObjects function. The function ignores and excludes invalid elements.

sortedList = slreportgen.utils.sortObjects(objectList,sortMethod) uses sortMethod to sort the elements in objectList.

example

Examples

collapse all

This example shows how to use the slreportgen.utils.sortObjects function to sort Simulink objects by depth.

Import these namespaces so you do not have to use long, fully qualified function and class names.

import slreportgen.finder.*
import slreportgen.utils.*

Load the Simulink model sortByDepthExampleModel.

model_name = "sortByDepthExampleModel";
load_system(model_name)

Use an slreportgen.finder.DiagramFinder object to find all the systems in the model, including the model itself. Then create an empty slreportgen.finder.BlockResult array.

sysArray = find(DiagramFinder(model_name));
unsortedList = BlockResult.empty;

Iterate through sysArray and use an slreportgen.finder.BlockFinder object to search for blocks in each subsystem. For each subsystem, append the search result block array to the end of the unsortedList array.

for idx = 1:length(sysArray)
  curBlockFinder = BlockFinder(sysArray(idx));
  curBlockFinder.BlockTypes = ["Inport","Gain","Outport"];
  curSysBlocks = find(curBlockFinder);
  unsortedList(end+1:end+length(curSysBlocks)) = curSysBlocks;
end

Display the paths of the blocks in unsortedList to see how they are sorted.

disp([unsortedList.BlockPath]');
    "sortByDepthExampleModel/In0"
    "sortByDepthExampleModel/Gain0"
    "sortByDepthExampleModel/Out0"
    "sortByDepthExampleModel/Sub1/In1"
    "sortByDepthExampleModel/Sub1/Gain1"
    "sortByDepthExampleModel/Sub1/Out1"
    "sortByDepthExampleModel/Sub1/Sub3/In3"
    "sortByDepthExampleModel/Sub1/Sub3/Gain3"
    "sortByDepthExampleModel/Sub1/Sub3/Out3"
    "sortByDepthExampleModel/Sub1/Sub3/Sub4/In4"
    "sortByDepthExampleModel/Sub1/Sub3/Sub4/Gain4"
    "sortByDepthExampleModel/Sub1/Sub3/Sub4/Out4"
    "sortByDepthExampleModel/Sub1/Sub5/In5"
    "sortByDepthExampleModel/Sub1/Sub5/Gain5"
    "sortByDepthExampleModel/Sub1/Sub5/Out5"
    "sortByDepthExampleModel/Sub2/In2"
    "sortByDepthExampleModel/Sub2/Gain2"
    "sortByDepthExampleModel/Sub2/Out2"

Use the slreportgen.utils.sortObjects function to sort the blocks in unsortedList by depth. Then display the paths of the sorted blocks to verify they are sorted by depth.

sortedList = sortObjects(unsortedList,"depth");
disp([sortedList.BlockPath]');
    "sortByDepthExampleModel/In0"
    "sortByDepthExampleModel/Gain0"
    "sortByDepthExampleModel/Out0"
    "sortByDepthExampleModel/Sub1/In1"
    "sortByDepthExampleModel/Sub1/Gain1"
    "sortByDepthExampleModel/Sub1/Out1"
    "sortByDepthExampleModel/Sub2/In2"
    "sortByDepthExampleModel/Sub2/Gain2"
    "sortByDepthExampleModel/Sub2/Out2"
    "sortByDepthExampleModel/Sub1/Sub3/In3"
    "sortByDepthExampleModel/Sub1/Sub3/Gain3"
    "sortByDepthExampleModel/Sub1/Sub3/Out3"
    "sortByDepthExampleModel/Sub1/Sub5/In5"
    "sortByDepthExampleModel/Sub1/Sub5/Gain5"
    "sortByDepthExampleModel/Sub1/Sub5/Out5"
    "sortByDepthExampleModel/Sub1/Sub3/Sub4/In4"
    "sortByDepthExampleModel/Sub1/Sub3/Sub4/Gain4"
    "sortByDepthExampleModel/Sub1/Sub3/Sub4/Out4"

Input Arguments

collapse all

List of Simulink® objects to sort, specified as one of these values:

Example: slreportgen.utils.sortObjects(["f14","f14/Actuator Model","f14/Aircraft Dynamics Model","sf_car"])

Example: slreportgen.utils.sortObjects(find_system("f14",findall=true))

Example: slreportgen.utils.sortObjects([find(slreportgen.finder.BlockFinder("sf_car")),find(slreportgen.finder.SignalFinder("sldemo_fuelsys"))])

Method for sorting, specified as one of these values:

  • "alphabetical" — Sort objects alphabetically by name.

  • "systemAlpha" — Sort objects alphabetically by parent system name.

  • "depth" — Sort objects by depth in the model hierarchy, where subsystems precede the subsystems they contain. For an example, see Sort Simulink® Objects by Depth.

Output Arguments

collapse all

Sorted list, returned as a string array, handle array, or search result object array. The returned array is the same type as objectList.

Version History

Introduced in R2022b