필터 지우기
필터 지우기

drawing lines between nodes in a GUI

조회 수: 3 (최근 30일)
vedesh Mohit
vedesh Mohit 2020년 2월 6일
편집: Adam Danz 2020년 2월 7일
Hey,
I would like to know if it is possible to add nodes to a user interface from GUIDE in matlab. I would like to place 24 nodes and the user would to be able to connect any two nodes in the interface. Can this be done?
  댓글 수: 4
Adam Danz
Adam Danz 2020년 2월 6일
The ginput allows the user to select the two nodes... have you looked into that documentation yet?
Adam Danz
Adam Danz 2020년 2월 6일
편집: Adam Danz 2020년 2월 7일
You've edited the question and it no longer asks how to connect two selected nodes with a new edge which is what my solution does.

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

답변 (1개)

Adam Danz
Adam Danz 2020년 2월 6일
Here's a demo that will get you started. You will have to tweak it to work for your data and your requirements.
You'll have to a button or define some kind of event that will prompt the user to select two nodes on the plot. That prompt should evoke a callback function that plots the graph, uses ginput to allow the user to select 2 nodes, and then replots the updated graph with the added edge.
% Plot the original graph
A(:,end) = 0;
names = {'alpha' 'beta' 'gamma' 'delta'};
G = graph(A,names,'upper','omitselfloops');
cla(handles.axes1)
ph = plot(handles.axes1, G);
axis equal
% prompt user to select 2 nodes
% cross hairs will appear allowing the user to click
% ANYWHERE on the GUI figure.
xy = ginput(2);
% find nearest nodes to each mouse click
% It would be wise to monitor the DISTANCE output and if it's greater than some value,
% you should reject the selection. The Distance is the distance between the mouse click
% and the nearest node.
nearestNodeIdx = nan(size(xy,1),1);
for i = 1:size(xy,1)
[distance, nearestNodeIdx(i)] = min(pdist2(xy(i,:), [ph.XData.', ph.YData.']));
end
% add edge to graph
H = addedge(G,nearestNodeIdx(1),nearestNodeIdx(2),0);
% Clear current graph and re-plot the updated one.
% Note that the graph may have changed orientation or shape.
delete(ph)
ph = plot(handles.axes1, H);

카테고리

Help CenterFile Exchange에서 Visual Exploration에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by