打印所有 Simulink 手动开关位置的 MATLAB 脚本
如何获取单个开关的位置
print_manual_switch_positions.m
get_param('MySimulinkModel/Manual Switch', 'sw')如果开关处于下方位置,这将返回 '0';如果处于上方位置,则返回 '1'。
打印示例:
print_example.m
fprintf('Manual Switch: %s\n', 'MySimulinkModel/Manual Switch');完整示例
以下 MATLAB 命令将递归迭代当前打开的 SIMULINK 模型中的所有手动开关,并将其当前位置打印到 MATLAB 命令窗口。
print_all_manual_switch_positions.m
% Find all Manual Switch blocks in the current model
current_model = bdroot;
manual_switches = find_system(current_model, 'BlockType', 'ManualSwitch');
% Output status of each Manual Switch block
for i = 1:length(manual_switches)
switch_handle = get_param(manual_switches{i}, 'Handle');
switch_pos = get_param(manual_switches{i}, 'sw');
% Display block path and status
if strcmp(switch_pos, '0')
position = 'Lower Position';
else
position = 'Upper Position';
end
fprintf('Manual Switch: %s\n', manual_switches{i});
fprintf('Status: %s (%s)\n\n', switch_pos, position);
end示例输出
manual_switch_print_output.txt
Manual Switch: MySimulinkModel/Manual Switch
Status: 0 (Lower Position)
Manual Switch: MySimulinkModel/Subsystem/My second switch
Status: 1 (Upper Position)Check 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