主要内容

本页采用了机器翻译。点击此处可查看最新英文版本。

slreportgen.utils.sortObjects

Simulink 对象排序

自 R2022b 起

说明

sortedList = slreportgen.utils.sortObjects(objectList) 按名称字母顺序对 objectList 中的元素进行排序。在使用 sortObjects 函数之前,您必须加载包含对象的模型。该函数忽略并排除无效元素。

sortedList = slreportgen.utils.sortObjects(objectList,sortMethod) 使用 sortMethodobjectList 中的元素进行排序。

示例

示例

全部折叠

此示例显示如何使用 slreportgen.utils.sortObjects 函数按深度对 Simulink 对象进行排序。

导入这些命名空间,这样您就不必使用长而完全限定的函数和类名。

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

加载 Simulink 模型 sortByDepthExampleModel

model_name = "sortByDepthExampleModel";
load_system(model_name)

使用 slreportgen.finder.DiagramFinder 对象查找模型中的所有系统,包括模型本身。然后创建一个空的 slreportgen.finder.BlockResult 数组。

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

遍历 sysArray 并使用 slreportgen.finder.BlockFinder 对象搜索每个子系统中的模块。对于每个子系统,将搜索结果模块数组附加到 unsortedList 数组的末尾。

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

显示 unsortedList 中模块的路径以查看它们是如何排序的。

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"

使用 slreportgen.utils.sortObjects 函数按深度对 unsortedList 中的模块进行排序。然后显示已排序模块的路径以验证它们是否按深度排序。

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"

输入参数

全部折叠

要排序的 Simulink® 对象列表,指定为以下值之一:

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

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

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

排序方法,指定为以下值之一:

  • "alphabetical" - 按名称将对象按字母顺序排序。

  • "systemAlpha" - 按父系统名称对对象进行字母排序。

  • "depth" - 按模型层次结构中的深度对对象进行排序,其中子系统位于其包含的子系统之前。有关示例,请参阅按深度对 Simulink® 对象进行排序

输出参量

全部折叠

排序列表,以字符串数组、句柄数组或搜索结果对象数组的形式返回。返回的数组与 objectList 类型相同。

版本历史记录

在 R2022b 中推出