I have the following graph:
s = [1 2 3 4 6 7 8 10];
t = [3 3 4 5 7 5 9 11];
G = digraph(s,t);
h = plot(G);
and want to add the following labels to the nodes
schedule = [0 0 1 2 3 1 2 2 3 2 3];
I am struggling to manage this with my basic Matlab knowledge. I want to display the node numbers (here: [1:1:11]) and the corresponding values from the schedule array.

 採用された回答

Voss
Voss 2022 年 12 月 6 日

1 投票

s = [1 2 3 4 6 7 8 10];
t = [3 3 4 5 7 5 9 11];
G = digraph(s,t);
h = plot(G);
% schedule = [0 0 1 2 3 1 2 2 3 2 3];
yd = get(h,'YData');
schedule = max(yd)-min(yd)-yd+1
schedule = 1×11
0 0 1 2 3 1 2 2 3 2 3
text(get(h,'XData')-0.1,yd,string(schedule), ...
'HorizontalAlignment','right', ...
'EdgeColor','k', ...
'FontSize',8)

4 件のコメント

Anna Schloti
Anna Schloti 2022 年 12 月 6 日
You did a great job, thanks a lot!
But unfortunately this is not the desired result...
My plan is to dynamically write the values from the schedule array to the respective nodes. The graph remains the same each time, the schedule array may change and contain different values depending on the algorithm (e.g. to illustrate the ASAP and ALAP algorithm).
Voss
Voss 2022 年 12 月 6 日
編集済み: Voss 2022 年 12 月 6 日
Ah, ok. In that case, use schedule as you want to define it:
s = [1 2 3 4 6 7 8 10];
t = [3 3 4 5 7 5 9 11];
G = digraph(s,t);
h = plot(G);
schedule = [0 0 1 2 3 1 2 2 3 2 3];
yd = get(h,'YData');
and return handles to the text objects (t) for subsequent modification:
t = text(get(h,'XData')-0.1,yd,string(schedule), ...
'HorizontalAlignment','right', ...
'EdgeColor','k', ...
'FontSize',8);
% this line is just for demo (to show two plots simultaneously), you don't need it:
copyobj(gca(),figure())
Then you can modify the text strings for new values of schedule as needed:
new_schedule = [9 19 29 21 33 11 2 62 53 42 33];
for ii = 1:numel(t)
set(t(ii),'String',new_schedule(ii));
end
Anna Schloti
Anna Schloti 2022 年 12 月 12 日
Thank you so much! This is exactly what I wanted to do!
Voss
Voss 2022 年 12 月 12 日
You're welcome!

サインインしてコメントする。

その他の回答 (1 件)

Maik
Maik 2022 年 12 月 6 日

1 投票

% You can use nodenames propertiest of digraph. However the names must be unique
s = [1 2 3 4 6 7 8 10];
t = [3 3 4 5 7 5 9 11];
weights = ones(1,8);
names = {'A' 'B' 'C' 'D' 'E' 'F' 'G 4' 'H 5' '1' '2' '3'};
G = digraph(s,t,weights,names)
G =
digraph with properties: Edges: [8×2 table] Nodes: [11×1 table]
h = plot(G)
h =
GraphPlot with properties: NodeColor: [0 0.4470 0.7410] MarkerSize: 4 Marker: 'o' EdgeColor: [0 0.4470 0.7410] LineWidth: 0.5000 LineStyle: '-' NodeLabel: {'A' 'B' 'C' 'D' 'E' 'F' 'G 4' 'H 5' '1' '2' '3'} EdgeLabel: {} XData: [1 2 1.5000 1.5000 2 2.5000 2.5000 3.5000 3.5000 4.5000 4.5000] YData: [4 4 3 2 1 3 2 2 1 2 1] ZData: [0 0 0 0 0 0 0 0 0 0 0] Show all properties

1 件のコメント

Anna Schloti
Anna Schloti 2022 年 12 月 6 日
Thanks for the answer, Maik.
I think my problem was misunderstood. I want as a result a graph with several labels at the nodes, so something like here:
Thanks in advance!

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeGraph and Network Algorithms についてさらに検索

質問済み:

2022 年 12 月 5 日

コメント済み:

2022 年 12 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by