Add Adapter programatically to system composer model

3 views (last 30 days)
Hello community,
i am trying to add an interface adapter to a system composer architecture programatically. I only can add components using addcomponent().
How is it possible to add a adaoter or do i need to convert the component into an adapter in some way? I am lost at the moment.
Thanks in advance!
Best Regards Andreas

Answers (1)

Josh Kahn
Josh Kahn on 22 Aug 2023
Edited: Josh Kahn on 22 Aug 2023
There is no API for creating adapters but you can create a component that performs the same actions using the SourceElement and DestinationElement Name-Value Arguments of the connect command. Here is an example:
% Load the architecture model and get the architecture where you want to
% add the components (in this case, the top-level).
a1Model = systemcomposer.loadModel('a1');
a1Architecture = a1Model.Architecture;
% Get the handles of the components and ports to be connected
txLeft = getComponent(a1Architecture, 'TX_Left');
txLeftOut = getPort(txLeft, 'TX_Out');
txRight = getComponent(a1Architecture, 'TX_Right');
txRightOut = getPort(txRight, 'TX_Out');
rx = getComponent(a1Architecture, 'RX');
rxIn = getPort(rx, 'RX_In');
% Create a component that will act as the adapter and add its architecture
% ports.
adapter = addComponent(a1Architecture, 'MyAdapter');
addPort(adapter.Architecture, 'TX_Out_Left', 'in');
addPort(adapter.Architecture, 'TX_Out_Right', 'in');
addPort(adapter.Architecture, 'RX_In', 'out');
% Get the corresponding component ports of the new adapter architecture ports.
adapterTXLeft = getPort(adapter, 'TX_Out_Left');
adapterTXRight = getPort(adapter, 'TX_Out_Right');
adapterRX = getPort(adapter, 'RX_In');
% Connect the component ports together
connect(txLeftOut, adapterTXLeft)
connect(txRightOut, adapterTXRight)
connect(adapterRX, rxIn)
% Clean up the diagram
Simulink.BlockDiagram.arrangeSystem('a1')
% Create the internal mappings inside of the architecture of the adapter.
% Could also have saved these from the return of the addPort command.
adapterTXLeftA = adapterTXLeft.ArchitecturePort;
adapterTXRightA = adapterTXRight.ArchitecturePort;
adapterRXA = adapterRX.ArchitecturePort;
connect(adapterTXLeftA, adapterRXA, DestinationElement='TX_Left')
connect(adapterTXRightA, adapterRXA, DestinationElement='TX_Right')
% Clean up the diagram
Simulink.BlockDiagram.arrangeSystem('a1/MyAdapter')
Let us know if you have any other questions!
Josh

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!