Finding Line Source and Destination Port and Blocks

47 次查看(过去 30 天)
Hello All
Please let me know how to find a proper way to do so. How can I get
SourceBlock, DestinationBlock, and the Port in the destination block where the line is going and Port port from where it is coming in the Source Block.
I created a class called LineInformation as shown below. The idea being for each line a Simulink model this class holds the Source and Destination Block as well as the Source and Destination port Name in the class.
classdef LineInformation
% LineInfornation: Provides all the properties of a line
% Detailed explanation goes here
properties
% Identifier of the Line
Identifier;
% Source Block of the Line
SourceBlock;
% Source Port of the Line
SourcePort;
% Detination Block of the line
DestinationBlock;
% Detination Port of the line
DestinationPort;
end
end
To find the class properties, I use the following function
function [allLineProperties] = GetLineInformation (systemName, systemDepth)
% function [allLineProperties] = GetLineInformation (systemName, systemDepth)
% Returns Objects of a Certain Model for a defined depth
% systemName: systemName name without extension
% systemDepth" Depth Level
% 0 for .slx file
% 1 for the Model Details
% 2 for Sub Systems Details
% Open simulink model vdp.mdl
open_system(systemName)
% Find all the line handles for this model
allLines = find_system(systemName, 'SearchDepth', systemDepth,'FindAll', 'On', 'type', 'line');
if (isempty(allLines))
return;
end
% Intialize Line Information Class
allLineProperties = LineInformation;
% Parse through lines and Assign the object properties.
for i = 1 : length(allLines)
allLineProperties(i).Identifier = allLines(i);
sourceData = get_param(allLineProperties(i).Identifier,'SrcBlockHandle');
destinationData = get_param(allLineProperties(i).Identifier,'DstBlockHandle');
sourcePortData = get_param(allLineProperties(i).Identifier,'SrcportHandle');
destinationPortData = get_param(allLineProperties(i).Identifier,'DstportHandle');
allLineProperties(i).SourceBlock = get_param(sourceData, 'Name');
allLineProperties(i).DestinationBlock = get_param(destinationData, 'Name');
allLineProperties(i).SourcePort = get_param(sourcePortData, 'Name');
allLineProperties(i).DestinationPort = get_param(destinationPortData, 'Name');
end
The problem is that, I do not get the DestinationPort or SourcePort they are always blank. (shown in the attachment).

回答(1 个)

Maverick
Maverick 2018-2-7
Hello
Please see the image for the relevance of source port and Dst port. The code is perfectly fine. The problem lies in the model. If the line connecting two blocks does not have any name, then the code
allLineProperties(i).SourcePort = get_param(sourcePortData, 'Name');
allLineProperties(i).DestinationPort = get_param(destinationPortData, 'Name');
will return empty string because they do not have any name associated to it. Please give some name to the line connecting two blocks in the model and you will not get an empty string

类别

Help CenterFile Exchange 中查找有关 Programmatic Model Editing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by