Main Content

slreportgen.utils.sortBlocks

Sort Simulink and Stateflow blocks

Description

example

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.

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 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 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® and Stateflow® blocks to sort, specified as one of these values:

ValueExample
String array of block paths
unsortedBlocks = ["slrgex_f14/Actuator Model",...
                  "slrgex_f14/Aircraft Dynamics Model",...
                  "slrgex_f14/alpha (rad)"];
Array of block handles
load_system("slrgex_f14");
unsortedList = find_system("slrgex_f14",...
                      findall=true,type="block");
Search result object array, returned by DiagramFinder, BlockFinder, and ChartDiagramFinder objects of the slreportgen.finder package
import slreportgen.finder.*
load_system("slrgex_sf_car");
load_system("slrgex_f14");
diagFinder = DiagramFinder("slrgex_f14");
blockFinder = BlockFinder("slrgex_sf_car");
unsortedBlocks = [find(diagFinder),find(blockFinder)];

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 a subsystem is preceded by the subsystem that contains it.
"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 to subrows.

The algorithm first groups the blocks to minimal rows and 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 to minimal columns and 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 only the nonvirtual blocks in model that contains the first block in the unsortedBlocks list, by their order of execution. Virtual blocks and blocks that are not in the model of the first block are appended to the end, unsorted. Blocks in multitasking systems are grouped 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