Main Content

slreportgen.utils.sortBlocks

Sort Simulink blocks

Since R2022b

Description

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

example

sortedList = slreportgen.utils.sortBlocks(unsortedBlocks,sortMethod) uses sortMethod to sort the elements in unsortedBlocks.

example

Examples

collapse all

This example shows how to use the slreportgen.utils.sortBlocks function to sort model blocks by their location in the model layout from left to right.

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

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

Open and observe the model to see how the blocks are divided to rows and how each row is sorted from left to right.

model_name = "sortLeftToRightDemoModel";
open_system(model_name);

Use an slreportgen.finder.BlockFinder object to find the block elements in the model. Save the list of blocks in the variable unsortedBlocks.

unsortedBlocks = find(BlockFinder(model_name));

Use the slreportgen.utils.sortBlocks function to sort the blocks with the sorting method leftToRight, and display the names of the sorted blocks. Verify that the blocks are sorted correctly.

sortedBlocks = sortBlocks(unsortedBlocks,"leftToRight");
disp([sortedBlocks.Name]');
    "1.1"
    "1.2"
    "1.3"
    "2.1"
    "2.2"
    "2.3"
    "3.1"
    "3.2"
    "3.3"
    "3.4"
    "3.5"
    "3.6"
    "3.7"

This example shows how to use the slreportgen.utils.sortBlocks function to sort model blocks by their location in the model layout from top to bottom.

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

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

Open and observe the model to see how the blocks are divided to columns, and how each column is sorted from top to bottom.

model_name = "sortTopToBottomDemoModel";
open_system(model_name);

Use an slreportgen.finder.BlockFinder object to find the block elements in the model. Save the list of blocks in the variable unsortedBlocks.

unsortedBlocks = find(BlockFinder(model_name));

Use the slreportgen.utils.sortBlocks function to sort the blocks with the sorting method topToBottom, and display the names of the sorted blocks. Verify that the blocks are sorted correctly.

sortedBlocks = sortBlocks(unsortedBlocks,"topToBottom");
disp([sortedBlocks.Name]');
    "1.1"
    "1.2"
    "1.3"
    "2.1"
    "2.2"
    "2.3"
    "3.1"
    "3.2"
    "3.3"
    "3.4"
    "3.5"
    "3.6"
    "3.7"

Input Arguments

collapse all

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

Example: slreportgen.utils.sortBlocks(["f14/Actuator Model","f14/Aircraft Dynamics Model","f14/alpha (rad)"])

Example: slreportgen.utils.sortBlocks(find_system("f14",findall=true,type="block"))

Example: slreportgen.utils.sortBlocks([find(slreportgen.finder.DiagramFinder("f14")),find(slreportgen.finder.BlockFinder("sf_car"))])

Method for sorting, specified as one of these values:

ValueDescription
"alphabetical"

Sort blocks alphabetically by name.

"systemAlpha"

Sort blocks alphabetically by parent system name.

"fullPathAlpha"

Sort blocks alphabetically by full block path.

"blockType"

Sort blocks alphabetically by block type.

"depth"Sort blocks by depth in the model hierarchy, where subsystems precede the subsystems they contain.
"leftToRight"

Sort objects by their location in the model layout using rows from left to right. A row of blocks in the model is a subgroup of blocks that are positioned between two horizontal lines. A minimal row is a row that cannot be divided into subrows.

The algorithm first groups the blocks into minimal rows and then sorts each row from left to right. The algorithm then concatenates the sorted rows from top to bottom.

For an example, see Sort Simulink® Blocks from Left to Right.

"topToBottom"

Sort objects by their location in the model layout using columns from top to bottom. A column of blocks in the model is a subgroup of blocks that are positioned between two vertical lines. A minimal column is a column that cannot be divided into subcolumns.

The algorithm first groups the blocks into minimal columns and then sorts each column from top to bottom. The algorithm then concatenates the sorted columns from left to right.

For an example, see Sort Simulink® Blocks from Top to Bottom.

"runtime"

Sort the nonvirtual blocks in the model that contains the first block in the unsortedBlocks list, by their order of execution. The function appends virtual blocks and blocks that are not in the model of the first block to the end of the list without sorting them. The function groups blocks in multitasking systems by the task in which they execute.

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 unsortedList.

Version History

Introduced in R2022b