How to programmatically know if a block is a sink block?
Show older comments
I have Simulink model. I want to know if each of the block inside my model is a sink block or not (or it belongs to a Sink Simulink library block or not )?
Currently I first get block type using get_param("blkname", 'BlockType') api. Then I try to map the blocktype to simulink library block .
For example, "Scope" maps to "simulink/sinks/Scope". This approach works for Simulink Block whose type is not Subsystem.
For example , for XYGraph which is a Sink block. get_param returns its blocktype as Subsystem.
Is there any alternative way to verify if a block is a source or a sink block ?
Accepted Answer
More Answers (2)
Sohil Shrestha
on 7 Mar 2021
Edited: Sohil Shrestha
on 8 Mar 2021
3 Comments
Paul
on 9 Mar 2021
I'm confused about your approach. Concepturally, how can the information in PortConnectivity be used to determine if the block is from the Sinks (or Sources) library?
Also, I don't understand this syntax
if size(ports_info{1,1},1) == 1
I thought ports_info would be an array of structures, so don't understand the brace indexing. But presumably the code runs, so I must have a misundertanding. Can you clarify?
Sohil Shrestha
on 9 Mar 2021
Pat Gipper's code uses the brace indexing as well, but I see an error:
>> ports_info=get_param(gcb,'PortConnectivity');
>> size(ports_info{1,1},1)
Brace indexing is not supported for variables of this type.
>> ports_info
ports_info =
struct with fields:
Type: '1'
Position: [100 95]
SrcBlock: 4.0520e+03
SrcPort: 0
DstBlock: []
DstPort: []
So I'm still confused about this.
More importantly, I don't see why checking for a single input or output is sufficent to check if a block is in the Sinks or Sources library. As you've already found, there are exceptions to this rule with From and Goto, and there are others, such as in the Model Verification library, not to mention blocks that can be defined as user defined library links that can have only one input or one output.
Pat Gipper
on 8 Mar 2021
I had to add some complexity to your code to make it work on my sample file.
modelfile = 'SinkXY';% Or whatever your model filename is?
load_system(modelfile);
blk0 = find_system(bdroot,'LookUnderMasks','on','FollowLinks','on');
%Upon digging into some api , this is the solution I came up with.
% I have no tested with all possible cases.
% If there are any pitfalls with this approach, I will appreciate any corrections.
[i,~] = size(blk0);
ptr_sinks=0;ptr_sources=0;
for ii=2:i
blk = blk0(ii);
src = get_param(blk,'Name');
ports_info = get_param(blk,'PortConnectivity');
block_type = get_param(blk, 'BlockType');
if sum(size(ports_info)) <= 2 && ~(strcmp(block_type,'From') || strcmp(block_type,'Goto'))
if size(ports_info{1,1},1) == 1
if(strcmp(block_type,'Scope') || any(size(ports_info{1,1}.SrcBlock))) % for Floating Scope
sinks{ptr_sinks+1} = src; %#ok<SAGROW>
ptr_sinks=ptr_sinks+1;
elseif (any(size(ports_info{1,1}.DstBlock)))
sources{ptr_sources+1} = src; %#ok<SAGROW>
ptr_sources=ptr_sources+1;
end
end
end
end
Categories
Find more on Subsystems in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!