Simulink:如何以编程方式设置所有选中信号的 TestPoint 状态
以下代码片段演示了如何在 Simulink 模型中使用 MATLAB,将当前所有选中信号线的 TestPoint 状态设置为 1(即启用测试点)。
它基于我们之前的文章 Simulink:如何以编程方式查询选中的信号/信号线。
set_testpoint_state.m
% 查找所有选中的信号线
lineHandles = find_system(gcs, 'FindAll', 'on', 'Type', 'line', 'Selected', 'on');
% 检查是否有选中的信号线
if isempty(lineHandles)
disp('No lines are currently selected.');
else
% 遍历所有选中的信号线并将其设置为测试点
for i = 1:length(lineHandles)
lineHandle = lineHandles(i);
% 将该信号线设置为测试点
set(lineHandle, 'TestPoint', 1);
% 可选:获取信号名称以供确认
signalName = get(lineHandle, 'Name');
if isempty(signalName)
fprintf('Line %d set as testpoint\n', i);
else
fprintf('Signal "%s" set as testpoint\n', signalName);
end
end
endmktestpoint.m
上述代码可以保存为名为 mktestpoint.m 的 MATLAB 脚本文件,方便重复使用:
mktestpoint.m
% mktestpoint.m
% 此脚本将当前所有选中信号线的 TestPoint 状态设置为给定值(1 为启用,0 为禁用)。
% 用法:在 Simulink 模型中选中信号线,然后运行此脚本。
function mktestpoint(enable)
% mktestpoint.m
% 此函数将当前所有选中信号线的 TestPoint 状态设置为给定值(1 为启用,0 为禁用)。
% 用法:在 Simulink 模型中选中信号线,调用 mktestpoint() 启用(默认)或 mktestpoint(0) 禁用。
if nargin < 1
enable = 1;
end
% 查找所有选中的信号线
lineHandles = find_system(gcs, 'FindAll', 'on', 'Type', 'line', 'Selected', 'on');
% 检查是否有选中的信号线
if isempty(lineHandles)
disp('No lines are currently selected.');
else
% 遍历所有选中的信号线并将其设置为测试点
for i = 1:length(lineHandles)
lineHandle = lineHandles(i);
% 将该信号线设置为测试点
set(lineHandle, 'TestPoint', enable);
% 可选:获取信号名称以供确认
signalName = get(lineHandle, 'Name');
if isempty(signalName)
fprintf('Line %d set as testpoint (%d)\n', i, enable);
else
fprintf('Signal "%s" set as testpoint (%d)\n', signalName, enable);
end
end
end
endCheck out similar posts by category:
MATLAB/Simulink
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow