How can I use scrpt to copy a componet(including port、subco​mponent、co​nnectors、s​tereotype.​..) from a Model to Other Model In SystemCompsoer?

3 views (last 30 days)
I want to build a component Library similar to Simulink Library in SystemComposer. The components within the component library include child components and internal connectors, etc., and apply specific stereotypes. I want to be able to copy the specific components in the library model file to the currently edited SystemComposer model in the form of a script to achieve reuse.
I looked up previous help and found that using linkToModel in combination with inlineModel could achieve an effect similar to copying. However, this requires me to treat each reusable component as an independent ArchitectureModel. I don't want this. I want to place these components in one model or multiple models.
I'm using R2022b version.

Answers (1)

Nithin
Nithin on 10 Jun 2025
Hi @Nature,
In MATLAB R2022b, there isn’t a modular or built-in method to deep copy System Composer components programmatically. Unlike Simulink, System Composer doesn’t offer native "library blocks". While "linkToModel" and "inlineComponentModel" can be used, they work only with separate architecture models and don’t offer a true copy functionality. Additionally, there is no built-in function like "copyComponent" that can recursively duplicate a component along with its internal hierarchy across models.
However, this behavior is simulated by writing a custom MATLAB script. This script will identify the source component within a library model and manually replicate its structure such as ports, subcomponents, connectors, and stereotypes, into a target model. The sample code below demonstrates how you can achieve this using iterative reconstruction
% Copy Ports
for port = sourceComp.Architecture.Ports
newComp.Architecture.addPort(port.Name, port.Direction);
end
% Copy Subcomponents
for sub = sourceComp.Architecture.Components
subCopy = newComp.Architecture.addComponent(sub.Name);
% Optional: Copy ports of subcomponent
for p = sub.Architecture.Ports
subCopy.Architecture.addPort(p.Name, p.Direction);
end
end
% Copy Connectors
conns = sourceComp.Architecture.Connectors;
for conn = conns
src = conn.SourcePort;
dst = conn.DestinationPort;
newSrc = findPortByName(newComp, src.Name);
newDst = findPortByName(newComp, dst.Name);
if ~isempty(newSrc) && ~isempty(newDst)
newComp.Architecture.connect(newSrc, newDst);
end
end
function port = findPortByName(comp, name)
ports = comp.Architecture.Ports;
port = [];
for p = ports
if strcmp(p.Name, name)
port = p;
return;
end
end
end
% Step 8: Copy Stereotypes
import systemcomposer.profile.*
st = getStereotype(sourceComp);
if ~isempty(st)
applyStereotype(newComp, st.QualifiedName);
end
In contrast, starting with MATLAB R2024a, System Composer now supports cut-and-paste of components across models using both the GUI and MATLAB commands. This enhanced capability allows you to transfer a component while preserving its ports, interfaces, requirement links, stereotypes, and UUIDs:
% Open source library and target model
open_system('libModel');
open_system('targetModel');
% Cut and paste programmatically
srcComp = find(component('libModel'), 'Name', 'LibComponent');
cut(srcComp);
paste;
save_system('targetModel');

Categories

Find more on System Composer in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!