How to create input and output port in Simulink and then connect them using Matlab script
82 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 ?
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 :)