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

4 Ansichten (letzte 30 Tage)
Monika Jaskolka
Monika Jaskolka am 1 Aug. 2020
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);

Akzeptierte Antwort

Christine Tobler
Christine Tobler am 3 Aug. 2020
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.

Weitere Antworten (0)

Kategorien

Mehr zu Graph and Network Algorithms finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by