How to use non-unique node names in a digraph?

4 次查看(过去 30 天)
I am trying to plot a Simulink model as a graph. I am using the digraph function, but I am running into issues because Simulink elements can have non-unique names. For example, a model can have any amount of subsystems named "Subsystem", so long as they are at different hierarchical levels. The same goes for Inports named "In1", Outports named "Out1", etc. And given that these are the default names for these elements, there typically are many elements with the same names.
A simple example of what I want to acheive is shown in the image. However, I receive an error saying node names must be unique. Is there a better way of plotting graphs with non-unique names displayed? I don't want to append suffixes to make the labels unique, because that would make the plot inaccurate.
s = [1 2 3];
t = [2 3 3];
w = ones(1, length(t));
n = {'Block Diagram', 'Subsystem', 'Subsystem'};
G = digraph(s, t, w, n, 'omitselfloops');
h = plot(G);

采纳的回答

Christine Tobler
Christine Tobler 2020-8-3
Instead of setting these inputs as node names, add them as a separate variable in the nodes table. Then, pass them to the plotting function as node labels:
G = digraph(s, t, w, [], 'omitselfloops');
G.Nodes.Label = n;
plot(G, 'NodeLabel', G.Nodes.Label);
The node names of a graph have to be unique because they can be used to specify a node as input to some functions (for example shortestpath). But this is not the case for the labels in the plot of a graph.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graph and Network Algorithms 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by