Skip to content

Instantly share code, notes, and snippets.

@luisdamed
Last active April 7, 2023 13:27
Show Gist options
  • Save luisdamed/8ec135ea0126ae3fdb8614c79d13d979 to your computer and use it in GitHub Desktop.
Save luisdamed/8ec135ea0126ae3fdb8614c79d13d979 to your computer and use it in GitHub Desktop.
Batch-create matching Simulink blocks based on the current selection. Read the article I wrote about this: https://www.makerluis.com/3-keyboard-macros-to-speed-up-your-simulink-workflow/
subsystem = gcs; % Get current system
objects = gsb; % Get selected blocks
% Alternative: find_system(gcs,'Selected','on')
for i_block = 1:numel(objects)
% Add Outports and connected lines for From blocks
if strcmp(get_param(objects{i_block}, 'BlockType'), 'From')
% Get the tag and position of the From block
tag = get_param(objects{i_block}, 'Gototag');
origin_position = get_param(objects{i_block}, 'Position');
% Define the destination of the new block
dest_block = [subsystem '/' tag];
y_line = origin_position(2) + (origin_position(4) - origin_position(2))/2;
dest_position = [origin_position(1) + 200 ...
y_line - 7 ...
origin_position(1) + 230 ...
y_line + 7 ];
% Add the new block and get its handle, then position it
dest_handle = add_block('built-in/Outport', dest_block, 'MakeNameUnique','on');
set_param(dest_handle, 'Position', dest_position);
% Add connecting line
OriginPortHandles = get_param(objects{i_block},'PortHandles');
DestPortHandles = get_param(dest_handle,'PortHandles');
line_handle = add_line(subsystem,OriginPortHandles.Outport(1),...
DestPortHandles.Inport(1));
set_param(line_handle, 'Name', tag)
end
% Add Gotos and connected lines for Input blocks
if strcmp(get_param(objects{i_block}, 'BlockType'), 'Inport')
% Get the tag and position of the Inpot block
name = get_param(objects{i_block}, 'Name');
origin_position = get_param(objects{i_block}, 'Position');
% Define the destination of the new block
dest_block = [subsystem '/Goto'];
y_line = origin_position(2) + (origin_position(4) - origin_position(2))/2;
dest_position = [origin_position(1) + 150 ...
y_line - 7 ...
origin_position(1) + 300 ...
y_line + 7 ];
% Add the new block and get its handle, then name it
dest_handle = add_block('built-in/Goto', dest_block, 'MakeNameUnique','on');
set_param(dest_handle, 'Gototag', name, 'Position', dest_position);
% Add connecting line
OriginPortHandles = get_param(objects{i_block},'PortHandles');
DestPortHandles = get_param(dest_handle,'PortHandles');
line_handle = add_line(subsystem,OriginPortHandles.Outport(1),...
DestPortHandles.Inport(1));
set_param(line_handle, 'Name', name)
end
% Add Froms for Goto blocks
if strcmp(get_param(objects{i_block}, 'BlockType'), 'Goto')
tag = get_param(objects{i_block}, 'Gototag');
origin_position = get_param(objects{i_block}, 'Position');
dest_block = [subsystem '/Goto'];
dest_position = origin_position + [200 0 200 0];
handle = add_block('built-in/from', dest_block, 'MakeNameUnique','on');
set_param(handle, 'Gototag', tag, 'Position', dest_position);
end
open_system(subsystem);
end
% Save as addblocks.m
% add to MATLAB Path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment