You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Simulink仿真中途修改参数:指定时间切换输入变量值的实现方法

方案实现步骤

一、模型端集成Assertion暂停逻辑

  • 打开test_all_InOut_Blocks.slx模型,从「Model Verification」库拖入1个Assertion模块
  • 拖入1个Clock模块输出当前仿真时间,将Clock的输出连接到Assertion模块的输入端口
  • 双击打开Assertion模块配置面板:
    1. 在「Assertion condition」输入框填写time < 3,仿真时间小于3秒时条件成立不触发断言,等于3秒时条件不成立触发断言
    2. 切换到「Callbacks」标签,选中「Assertion callback」项,填入回调代码:
set_param(bdroot,'SimulationCommand','pause');
disp(sprintf('\nSimulation paused.'))
  1. 勾选「Enable assertion」,取消勾选「Stop simulation when assertion fails」,避免触发断言后直接终止仿真,保存配置即可

二、MATLAB脚本调整

将你的控制脚本修改为如下内容即可实现需求:

save_mat_file_InOut_blocks; % 生成工作区所需的所有变量
% 初始化input_boucle初始值为0
input_Boucle.signals.values = double(0); 

t_stop_ouverte = 5;
T_s = t_stop_ouverte/1000;
options_boucle_ouverte = simset('SrcWorkspace','current', 'fixedstep', T_s) ;
% 启动仿真,运行到3秒时会被Assertion回调自动暂停
sim('test_all_InOut_Blocks.slx',t_stop_ouverte, options_boucle_ouverte);

% 仿真暂停后修改输入值为1
input_Boucle.signals.values = double(1);
% 继续仿真直到5秒结束
set_param('test_all_InOut_Blocks','SimulationCommand','continue');

更简便的替代方案

如果不需要特意暂停仿真,你可以直接在工作区构造随时间跳变的输入信号,不需要修改模型加任何模块:

% 构造3秒跳变的时间序列输入
t = [0,3,5];
val = [0,1,1];
input_Boucle = timeseries(val,t);

之后直接调用你最初的仿真代码即可,仿真会自动在3秒时切换输入值。

内容的提问来源于stack exchange,提问作者elle.delle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 19:45:07