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

13 ビュー (過去 30 日間)
Monika Jaskolka
Monika Jaskolka 2020 年 8 月 1 日
回答済み: Christine Tobler 2020 年 8 月 3 日
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 Center および File ExchangeGraph 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