Obtaing a Node's ID

조회 수: 2 (최근 30일)
Tariq Atieh
Tariq Atieh 2020년 2월 19일
편집: Srijith Kasaragod 2021년 9월 3일
I have a node graph that randomly changes each time. I would like it so when I click on the node graph it selected the clicked node and outs the node ID as a variable. For example, I would like it so when you click on the nodes with nodeID 4 3 and 6. It retruns a variable containing the numbers 4 3 and 6. Thank you for your time.
  댓글 수: 2
KSSV
KSSV 2020년 2월 19일
Show us how the graph is...how did you plot the graph?
Tariq Atieh
Tariq Atieh 2020년 2월 19일
numNodes = 64;
numEdge = numNodes;
p = rand(numNodes,2);
st = unique( sort( randi(numNodes, numEdge), 2), 'rows');
G = graph(st(:,1), st(:,2));
gp = plot(G);
gp.XData = p(:,1); gp.YData = p(:,2);
This is my code its just a basic node graph. I just want a way sio that when I click on a node or nodes it puts the NodeIDs in a matrix. Like I click on nodes 1 and 2 I get a matrix of 1 and 2

댓글을 달려면 로그인하십시오.

답변 (1개)

Srijith Kasaragod
Srijith Kasaragod 2021년 9월 3일
편집: Srijith Kasaragod 2021년 9월 3일
Following piece of code shows one possible way to do the same. A button had been added to the figure. Upon selecting required nodes in the figure and clicking on button node labels will be copied and displayed in the command window.
h = figure;
ax = axes(...
'Parent', h,...
'Units', 'normalized',...
'Position', [0.1 0.1 0.8 0.6]);
%define sample graph
s={'A','A','B','C'};
t={'B','C','C','D'};
G= graph (s,t)
ph= plot(G);
%add a button to the figure
out= uicontrol(...
'Parent', h,...
'Units', 'normalized',...
'Position', [0.1 0.8 0.4 0.1],...
'String', 'Enter',...
'Callback', {@Callback_pb,h,ph});
%button callback function definition
function Callback_pb(~,~,h,ph)
d = datacursormode(h);
vals = getCursorInfo(d);
output={};
len_temp= size(vals);
len=len_temp(2);
for i=1:len
x= vals(i).Position;
output{end+1}= ph.NodeLabel{find(ph.XData==x(1) & ph.YData==x(2))};
end
%required output cell array
output
end
Refer this link to read more about uibutton.

카테고리

Help CenterFile Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by