How to find number of input and output ports of an unknown simulink model ?

163 ビュー (過去 30 日間)
Shiv Nileshkumar Matliwala
Shiv Nileshkumar Matliwala 2022 年 11 月 24 日
コメント済み: Chong Wang 2023 年 6 月 19 日
I have three different simulink models Model1, Model2 and Model3. Now I am adding these all models into one single blank Model.
Here, I dont know that how many input ports are there and how many output ports are there.
So to connect the Model1, Model2 and Model3, I need to identify first how many input ports and output ports are there in these all three models Model1, Model2, Model3.
Then suppose first model has input ports like A and B and The second model has output ports like A and E
Then I want to do the 'for' loop which will one by one check all the input ports with all the output ports and when the port name like A matches with any output port name A then that input port and output port should be connected.
Everything I need to do it with matlab script.
so, any idea How to do this or any suggetions how can I find the total number of input and output ports of the simulink model ?
Please help me in this.
Thank you.

採用された回答

Paul
Paul 2022 年 11 月 24 日
Hi Shiv,
I started out with this model with three subsystems.
Then I ran this code:
% Handles to the subsystems
subsystems = find_system(get_param(gcs,'Handle'),'BlockType','SubSystem');
% Handes to the Inport blocks of the subsystems
inports = find_system(subsystems,'BlockType','Inport');
% Names of the Inport blocks of the subsystems
inportnames = get_param(inports,'Name');
% Handles to the Outport blocks of the subsystems
outports = find_system(subsystems,'BlockType','Outport');
% Names of the Outport blocks of the subsystems
outportnames = get_param(outports,'Name');
% Cell array of structures of port handles to the ports of the subsystems
porthandles = get_param(subsystems,'PortHandles');
% Convert to structure array
porthandles = [porthandles{:}];
% Vector of handles to the input ports
inporthandles = [porthandles.Inport];
% Vector of handles to the output ports
outporthandles = [porthandles.Outport];
% match up the port names. Assumes any matches between inport names and outport names are 1-to-1
[Lia,LocB]=ismember(inportnames,outportnames);
% connect the ports
add_line(gcs,outporthandles(LocB(Lia)),inporthandles(Lia));
The result was this model
Maybe this code can be adapted to your needs.
  9 件のコメント
Shiv Nileshkumar Matliwala
Shiv Nileshkumar Matliwala 2022 年 12 月 12 日
Hello Paul,
I just need some help from you. Can yu please tell me how can I get the data type of a signal in simulink model programmatically ??
for example, I have 2 signals A and B between Bus selector and bus creator blocks in simulink model. Now I want to find the data type of signals A and B.
Can you suggest me how can I find this data types and how can I set that with 'Boolean' ?
Chong Wang
Chong Wang 2023 年 6 月 19 日
cool

サインインしてコメントする。

その他の回答 (1 件)

Florian Bidaud
Florian Bidaud 2022 年 11 月 24 日
編集済み: Florian Bidaud 2022 年 11 月 24 日
Hi,
You need to use the function get_param with 'Ports' as a parameter, for each subsystem of your model.
The first and second element of the array will give you the input and output ports numbers
load_system('system.slx')
ports_data = get_param('system/Subsystem','Ports')
input_ports_number = ports_data(1)
output_ports_numer = ports_data(2)
  4 件のコメント
Shiv Nileshkumar Matliwala
Shiv Nileshkumar Matliwala 2022 年 11 月 24 日
I had one more doubt that if i have one unknown simulink model for which i dont dont how many blocks are there. So is there any way I can find like how many blocks are there inside the simulink model ??
Florian Bidaud
Florian Bidaud 2022 年 11 月 24 日
Yes, you can again use :
length(get_param('yourSimulinkSystem','Blocks'))

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeProgrammatic Model Editing についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by