通过合并多个 Interpolation Using Prelookup 模块来提高代码效率
您可以使用模型转换器工具重构建模模式,以提高生成的代码的效率。模型转换器识别并合并多个 Interpolation Using Prelookup 模块,这些模块具有相同的输入信号,并且这些信号是从 Prelookup 模块的输出端连接的,合并成一个 Interpolation Using Prelookup 模块。
如果 Interpolation Using Prelookup 块的属性除了表数据之外都相同,则模型转换器可以正常工作。减少模型中的 Interpolation Using Prelookup 块的数量可以减少代码中的变量赋值次数,从而提高生成代码的效率。您可以使用模型转换器应用程序或编程命令来重构模型。
模型变换器可以替换多个 Interpolation Using Prelookup:
将相同的输入信号连接到具有相同索引和分数参数的 Prelookup 模块
将输出信号连接到同一个 Multiport Switch 模块
具有相同的断点规范、值和数据类型
具有相同的算法参数
分数参数应采用相同的数据类型
使用模型转换器应用程序合并 Interpolation Using Prelookup 块
本示例展示了如何使用模型转换器识别冗余的 Interpolation Using Prelookup 块,然后重构模型。

模型 ex_interpolation_optimize 使用 Prelookup 模块向多个 Interpolation Using Prelookup 模块输入信号。这些 Interpolation 模块的输出连接到 Multiport Switch 模块。
在这个示例中,您可以识别符合转换条件的 Interpolation Using Prelookup 块,并将它们替换为单个 Interpolation Using Prelookup 模块和连接到 Multiport Switch 模块的 Constant 块。
将模型保存到您的工作文件夹中。
在应用选项卡上,点击模型转换器。
在 Transformations 文件夹中,选中 替换 Interpolation Using Prelookup 块 复选框。
选择从此变换中跳过库中的插值-ND 块选项,以避免替换链接到库的 Interpolation Using Prelookup 块。
在转换模型的前缀字段中,指定重构模型的前缀。
点击运行此检查。顶部的结果表包含指向 Interpolation Using Prelookup 块和相应的 Multiport Switch 模块端口索引的超链接。
清除候选组下方的复选框,以识别您不想转换的组。
点击重构模型。结果表包含指向新模型的超链接。该表包含指向共享的 Interpolation Using Prelookup 块和相应的 Multiport Switch 模块端口的超链接。该工具还会创建一个
m2m_ex_interpolation_optimize文件夹,其中包含新的gen_ex_interpolation_optimize.slx模型。

gen_ex_interpolation_optimize.slx 模型中的两个 Prelookup 块 Prelookup 和 Prelookup1 连接到单个 Interpolation Using Prelookup 模块,而 Constant 块连接到 Multiport Switch 模块端口,以提供表格数据作为输入。

以编程方式合并 Interpolation Using Prelookup 块
要以编程方式使用模型转换器,请使用:
将模型
ex_interpolation_optimize保存到当前工作目录。要确定符合转换条件的候选人,请使用函数
Simulink.ModelTransform.CommonSourceInterpolation.identifyCandidates创建对象transformResults。transformResults = Simulink.ModelTransform.CommonSourceInterpolation.identifyCandidates('ex_interpolation_optimize')transformResults = Results with properties: Candidates: [1×1 struct]transformResults对象有一个属性Candidates,它是一个包含两个字段InterpolationPorts和SwitchPorts的结构体。transformResults.Candidates = struct with fields: InterpolationPorts: [4×1 struct] SwitchPorts: [4×1 struct]查看
InterpolationPorts字段。transformResults.Candidates.InterpolationPorts =
4×1 struct array with fields: Block PortInterpolationPorts字段由两个数组Block和Port组成。同样,SwitchPorts也具有相同的属性。将
Candidates.InterpolationPorts和Candidates.SwitchPorts字段转换为表。struct2table(transformResults.Candidates.InterpolationPorts) struct2table(transformResults.Candidates.SwitchPorts)
ans = 4×2 table Block Port _____________________________________________ ____ {'ex_interpolation_optimize/Interpolation↵Using Prelookup' } 0 {'ex_interpolation_optimize/Interpolation↵Using Prelookup1'} 0 {'ex_interpolation_optimize/Interpolation↵Using Prelookup2'} 0 {'ex_interpolation_optimize/Interpolation↵Using Prelookup3'} 0 ans = 4×2 table Block Port ______________________________ ____ {'ex_interpolation_optimize/Multiport_Switch'} 1 {'ex_interpolation_optimize/Multiport_Switch'} 2 {'ex_interpolation_optimize/Multiport_Switch'} 3 {'ex_interpolation_optimize/Multiport_Switch'} 4使用
SwitchPorts查看哪些 Interpolation Using Prelookup 块连接到哪些 Multiport Switch 模块端口。要重构模型,请使用函数
Simulink.ModelTransform.CommonSourceInterpolation.refactorModel。此函数使用来自identifyCandidate函数的对象transformResults。refactorResults = Simulink.ModelTransform.CommonSourceInterpolation.refactorModel(transformResults)
refactorResults = RefactorResults with properties: ModelName: 'ex_interpolation_optimize' ModelDirectory: '' TraceabilityInfo: [4×1 containers.Map]refactorResults对象的ModelName和ModelDirectory属性列出了重构模型的名称和位置。TraceabilityInfo是一个containers.Map对象,它列出了模块跟踪信息。
条件和限制
如果出现以下情况,模型转换器无法替换 Interpolation Using Prelookup 块:
Interpolation Using Prelookup 模块位于注释区域或非非活动变体中。
Interpolation Using Prelookup 块已被屏蔽。
模型转换器应用程序不会在原子子系统、引用模型或库链接块的边界上替换 Interpolation Using Prelookup 块。