How to create input and output port in Simulink and then connect them using Matlab script
106 ビュー (過去 30 日間)
古いコメントを表示
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 ?
0 件のコメント
採用された回答
Luca Ferro
2023 年 3 月 27 日
編集済み: Luca Ferro
2023 年 3 月 27 日
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 件のコメント
Luca Ferro
2023 年 4 月 7 日
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:
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Naming Conventions についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!