主要内容

编程式模型编辑

使用 API 执行建模基础操作,例如添加、配置和连接模块

您可以通过编程方式创建、编辑、搜索和检查 Simulink® 模型。要以编程方式与模型交互,必须已加载模型。创建或打开模型也会加载模型。使用“创建模型”下列出的函数来创建、加载或打开模型。然后,使用本页上的其他函数以编程方式编辑模型及其内容。在函数输入参量中,将希望函数编辑的对象指定为句柄、路径或文件名。此目标对象可以是模型、子系统或库等组件,或者是模块或信号线等模型元素。您可以指定已知其名称和位置的目标对象,也可以通过编程方式搜索符合特定条件的目标对象。有关详细信息,请参阅函数文档和Get Handles and Paths

当您要与第三方共享模型而又不泄露知识产权时,请使用 Simulink.ModelReference.protect (Embedded Coder) 函数创建模型的受保护版本。有关详细信息,请参阅保护模型以隐藏内容 (Embedded Coder)

有关如何以编程方式运行仿真的信息,请参阅以编程方式运行仿真。要以编程方式创建和编辑工程,请参阅以编程方式创建和编辑工程。要以编程方式配置 Simulink 编辑器,请参阅Programmatic Model Editor Appearance Parameters。要从 MATLAB® 命令行窗口调试仿真,请参阅以编程方式调试仿真

函数

全部展开

getSimulinkBlockHandle从模块路径中获取模块句柄
getfullname获取标识模块或信号线的路径
get_param获取参数名称和值
gcs获取当前系统的路径名称
gcb获取当前模块的路径名称
gcbh获取当前模块的句柄
gcbp获取当前模块的 Simulink.BlockPath 对象
bdroot当前系统的顶层模型
getCurrentAnnotation获取当前注解对象
getCallbackAnnotationGet annotation executing callback

查找模型元素

find_system查找模型、模块、信号线、端口和注解
Simulink.findBlocksSimulink 模型中查找模块
Simulink.findBlocksOfTypeSimulink 模型中查找指定类型的模块
Simulink.FindOptions指定在模型和子系统中查找模块的选项
hilite_system突出显示模块、信号线、端口或注解

查找模型

find_system查找模型、模块、信号线、端口和注解
Simulink.allBlockDiagrams查找加载的 Simulink 模型和库
modelfinderSearch and open examples, models, and projects (自 R2022a 起)
modelfinder.importDatabaseImport database to Model Finder (自 R2023b 起)
modelfinder.createDatabaseCreate new database to index models (自 R2023b 起)
modelfinder.setDefaultDatabaseSet default database to index models (自 R2023b 起)
modelfinder.setSearchDatabaseSet search scope to find models (自 R2023b 起)
modelfinder.searchFilterCreate Model Finder search filter (自 R2025a 起)
modelfinder.registerFolderIndex models in Model Finder (自 R2022a 起)
modelfinder.unregisterFolderRemove models from Model Finder (自 R2022a 起)
modelfinder.deleteDatabaseRemove database from Model Finder (自 R2023b 起)

获取模型元数据

Simulink.MDLInfo在不加载文件的情况下提取 SLX、SLXP 或 MDL 文件信息
Simulink.MDLInfo.getDescription提取 SLX、SLXP 或 MDL 文件描述而不加载文件
Simulink.MDLInfo.getMetadata提取 SLX、SLXP 或 MDL 文件元数据而不加载文件

启动 Simulink

simulink打开 Simulink 起始页
start_simulink启动 Simulink 而不打开任何窗口
isSimulinkStarted检查 Simulink 是否已启动
slLibraryBrowser打开、加载并关闭 Simulink 库浏览器,创建并获取库浏览器对象的句柄

创建、加载、打开、保存和关闭模型

new_system在内存中创建 Simulink 模型或库
load_systemSimulink 模型加载到内存中
open_system打开模型、库、子系统或模块对话框
save_system保存 Simulink 模型
close_system关闭 Simulink 模型窗口或模块对话框
bdclose无条件关闭任何或所有 Simulink 模型窗口

添加、替换和删除模块

add_block向模型中添加模块
addterms向模型中未连接的端口添加 Terminator 模块
replace_block替换 Simulink 模型中的模块
delete_blockSimulink 系统中删除模块
Simulink.BlockDiagram.deleteContents删除模型的图内容
Simulink.SubSystem.deleteContents删除子系统的内容

连接模块

Simulink.connectBlocksConnect blocks with signal lines (自 R2024b 起)
add_lineSimulink 模型中添加信号线
delete_lineSimulink 模型中删除信号线

编辑参数

get_param获取参数名称和值
set_param设置 Simulink 参数值
add_paramAdd parameter to Simulink model
delete_paramDelete model parameter added with add_param function
docblockGet or set editor invoked by Simulink DocBlock block

配置模型布局

Simulink.BlockDiagram.arrangeSystem改进模块图布局
Simulink.BlockDiagram.routeLineRoute existing lines of model
Simulink.BlockDiagram.resizeBlocksToFitContentAdjust block size to fit displayed value (自 R2024b 起)

将模型元素分组为子系统

Simulink.BlockDiagram.createSubsystem创建包含指定模块集的子系统
Simulink.BlockDiagram.expandSubsystemReplace subsystem with subsystem contents
Simulink.SubSystem.copyContentsToBlockDiagram将图形内容从子系统复制到另一个模型
Simulink.SubSystem.deleteContents删除子系统的内容
bdIsSubsystemDetermine whether model is subsystem
isSimulinkStarted检查 Simulink 是否已启动
bdIsLoaded确定是否加载了模型、子系统或库
bdIsDirtyDetermine whether model, subsystem, or library has unsaved changes
slIsFileChangedOnDiskDetermine whether model has changed since it was loaded
bdIsLibrary确定模型是否为库
bdIsSubsystemDetermine whether model is subsystem
edittime.getDisplayIssuesCheck whether model design warnings and errors are on
edittime.setDisplayIssuesDetect model design errors and warnings

使用模型模板

Simulink.exportToTemplateCreate template from model or project
Simulink.createFromTemplateCreate model or project from template
Simulink.findTemplatesFind model or project templates with specified properties
Simulink.defaultModelTemplate设置或获取默认模型模板

导出模型

Simulink.ModelReference.protectObscure referenced model contents to hide intellectual property
Simulink.exportToVersion导出模型、库或工程,用于 Simulink 的以前版本
Simulink.exportToTemplateCreate template from model or project
slCharacterEncoding指定要在从 Simulink 模型生成的代码中使用的编码

打印模型

frameeditOpen PrintFrame Editor to edit print frames for Simulink and Stateflow block diagrams
orient打印或保存时使用的纸张方向
print打印图窗或保存为特定文件格式

对象

ModelFinderFilterModel Finder search filter (自 R2025a 起)
Simulink.Annotation创建并指定文本注解、图像注解和区域注解的属性

主题

以编程方式指定要编辑的对象

创建模型

格式化模型

共享模型