How to create input and output port in Simulink and then connect them using Matlab script

112 views (last 30 days)
I want to create input and output ports in Simulink and then connect them using Matlab script to begin with iplementation of something bigger.
I tried searching on Google and Matlab central but couldnt find the relevant information.
Can you guide me to relevant links to begin with and some commands which will help me , a small sample script will be of great help.
Also would it be possible to add a multiplication block in simulink which takes input from two inports and then send output to a display block and also an output port ?

Accepted Answer

Luca Ferro
Luca Ferro on 27 Mar 2023
Edited: Luca Ferro on 27 Mar 2023
Here is a link to the relevant mathworks documentation: https://ch.mathworks.com/help/simulink/ug/approach-modeling-programmatically.html
In any case, adding blocks is quite simple. The function is add_block(librarySource,simulinnkDestination):
librarySource is from where the block comes, you can either search the source of any simulink block in the documentation or in simulink itself go to the library browser (under simulation ribbon) and hoover on the block you wish to add to be shown the source.
simulinkDestination is the path where you want to block to be created. Note that the name MUST be unique.
In your case:
add_block('simulink/Commonly Used Blocks/In1','yourSimulinkProjectName/yourInputName'); %for inports
add_block('simulink/Commonly Used Blocks/Out1','yourSimulinkProjectName/yourOutputName'); %for outports
add_block('simulink/Commonly Used Blocks/Product','yourSimulinkProjectName/product1'); %for multiplication
add_block('simulink/Commonly Used Blocks/Scope','yourSimulinkProjectName/scope1'); %for display
for adding lines there are several mehods you could use, the one i like is using port handles which are numbers that identifies the ports of each block. This is valid for every block, here i'll show you for the before said input and product:
inputHndl = get_param('yourSimulinkProjectName/yourInputName','PortHandles');
productHndl = get_param('yourSimulinkProjectName/product1','PortHandles');
The first string is the path to the block, the second is the property you want to get. The output is a struct of which the two fields you are mostly interested into are .Inport and .Outport.
Now that you have the handles you can connect the blocks:
add_line('yourSimulinkProjectName',inputHndl.Outport(1),productHndl.Inport(1),,'autorouting','smart')
The first string is the project name, and note that the handles have numbers. For the input we only have one because there is only one port, so 1 is the only value we could choose. For the product there is 2 inports, i choose one but it could have been productHndl.Inport(2) if you wished. The autorouting is just for practical purposes but it's totally optional, it just avoids overlapping.
Repeat the same for every block and there you have the connection.
At the end i would suggest to use this command so that it manages the positioning of the blocks automatically. Otherwise you would have to move them programmatically using set_param() but it can be a pain.
Simulink.BlockDiagram.arrangeSystem('yourSimulinkProjectName')
For the script, try it yourself before and get back at me if you have any issues :)
  5 Comments
ANKUR WADHWA
ANKUR WADHWA on 7 Apr 2023
Thanks for the new command @Luca Ferro , in the code snippet I shared in previous comment I am renaming the signal line between the ports once the connection is made which I am doing by
% Name or rename the line connected between the ports
speedline = get_param(inputHndlspeed.Outport,'Line');
set_param(speedline,'Name','speed');
I was trying to find a way where this can be done without creating intermediate variables and can be done while creating the connection between the ports. Is there a possible way to achieve the same
Or we have to mandatorily write additional commands for naming the signal line only, post creating the connection.
Where we have to first get the port or block handle and then get Line properties separately and then use the "Name" attribute to name the signal
Luca Ferro
Luca Ferro on 7 Apr 2023
as you can see from the get_param, the line is not an object himself but it refers to the outport from which is originated. I'm not aware of a way to use a one-liner to set the name. If there is a way it will come in the shape of name-value options on the creation of the block (speed in your case). You can search for block specific parameters here:

Sign in to comment.

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!