Effacer les filtres
Effacer les filtres

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

79 vues (au cours des 30 derniers jours)
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 ?

Réponse acceptée

Luca Ferro
Luca Ferro le 27 Mar 2023
Modifié(e) : Luca Ferro le 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:
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.
For the script, try it yourself before and get back at me if you have any issues :)
  5 commentaires
ANKUR WADHWA le 7 Avr 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');
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 le 7 Avr 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:

Connectez-vous pour commenter.

Plus de réponses (0)


En savoir plus sur Simulink Functions dans Help Center et File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by