필터 지우기
필터 지우기

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

조회 수: 2 (최근 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 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