MATLAB script to print all Simulink manual switch positions
How to get the position of a single switch
get_param('MySimulinkModel/Manual Switch', 'sw')
This will return '0'
if the switch is in the lower position and '1'
if it is in the upper position.
Example how to print it:
fprintf('Manual Switch: %s\n', 'MySimulinkModel/Manual Switch');
Full example
The following MATLAB command will iterate all manual switches in the currently opened SIMULINK model recursively and print their current positions to the MATLAB command window.
% 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
Example output
Manual Switch: MySimulinkModel/Manual Switch
Status: 0 (Lower Position)
Manual Switch: MySimulinkModel/Subsystem/My second switch
Status: 1 (Upper Position)
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow