Obtain Simulink Object Handle (or path) from slxmlcomp results?

3 Ansichten (letzte 30 Tage)
Hey there - any help would be greatly appreciated.
I am working with versioning/differences between versions of models, and I am wondering if it is possible to get either the handle of the actual block (or the full path to search for the block using find_system) from the results of the xml comparison done using slxmlcomp in Simulink?
Essentially, I use the comparison function (slxmlcomp) to obtain the list of differences, which returns a list of Nodes, and I want to be able to interact with the model elements using a small program. However, the only properties I have access to as a result of the comparison are as follows (see example):
cur =
Node with properties:
Children: []
Edited: 1
Name: 'Comparison1'
Parameters: [1x1 struct]
Parent: [1x1 xmlcomp.Node]
Partner: [1x1 xmlcomp.Node]
I have tried reconstructing the paths by adding the parents all the way up, but here the 'parent' is in the tree of differences, not the actual model elements, so I am at a loss.
Help?

Akzeptierte Antwort

Monika Jaskolka
Monika Jaskolka am 30 Jan. 2018
Bearbeitet: Monika Jaskolka am 30 Jan. 2018
There is no built-in way of getting the handle/path. As you mentioned, it is possible to reconstruct the path information given the tree structure, removing any artifacts of the Edits object (e.g. 'Comparison Root', 'Simulink'). I wrote the following "getPath" function to construct the path of the node and then double check that the model contains the block.
function path = getPath(node, sys)
% GETPATH Get the path of a node in a model. Note: Not all elements in a
% model have a Path parameter (e.g. lines, annotations).
%
% Inputs:
% node xmlcomp.Node object, representing a block.
% sys Path or name of the model.
%
% Outputs:
% path Path of node in the model.
% Validate inputs
try
assert(isa(node, 'xmlcomp.Node'))
catch
message = 'Node argument must be an xmlcomp.Node object.';
error(message)
end
sys = char(sys);
path = assemblePath(sys, node);
try % Confirm that the model does in fact have an element with this path
[~,name,~] = fileparts(sys);
sysLoaded = bdIsLoaded(bdroot(name));
if sysLoaded && (getSimulinkBlockHandle(path) == -1)
path = {};
end
catch
% Model not loaded so cannot check
warning('off', 'backtrace');
message = ['Model ' name ' is not loaded. Could not check if ' path ...
' exists in system ' sys '. Path returned is a best guess based'...
' on the xmlcomp.Edits object.' newline];
warning(message)
end
end
function path = assemblePath(sys, node)
% ASSEMBLEPATH Recursively assemble the path string from the
% xmlcomp.Edits object. Any comparison artifacts are omitted
% (e.g. 'Comparison Root').
%
% Inputs:
% sys Path or name of the model.
% node xmlcomp.Node object, representing a block, for which
% to find the path.
%
% Outputs:
% path Path of the node.
if strcmp(node.Name, 'Comparison Root')
path = ''; % Omit
elseif strcmp(node.Name, 'Simulink')
[~,name,~] = fileparts(sys);
path = name; % Use actual model name
else
path = [assemblePath(sys, node.Parent) '/' node.Name];
end
end
Other elements in a model, such as lines and annotations, don't have paths so this will not work in their case. It is possible to write a "getHandle" function that first determines the type of node (i.e. whether it represents a block, line, etc.) based on its parameters/naming convention and then:
  1. if node is a block, hdl = get_param(getPath(node, sys), 'Handle');
  2. if node is a line, get its parent path and find all lines in that subsystem, hdl = the line whose source block/destination blocks and port numbers match those given in the node's Name parameter (which is in the form 'SrcBlock:PortNum -> DstBlock:PortNum').
  3. if node is an annotation, get its parent path, find all annotations in the subsystem, hdl = the annotation whose 'Text' parameter matches the node's Name parameter.
  4. and so on...
  2 Kommentare
Eric
Eric am 30 Jan. 2018
Thanks - this is essentially what I ended up doing to solve the problem. I had just hoped there was a more reliable built-in way.
Monika Jaskolka
Monika Jaskolka am 20 Jun. 2019
I recently released a collection of scripts on the File Exchange that help with programmatically supporting operations on the xmlcomp.Edits comparison object: https://www.mathworks.com/matlabcentral/fileexchange/71834-model-comparison-utility

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Schedule Model Components finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by