Attach buttons to graph elements

조회 수: 26 (최근 30일)
Timothy
Timothy 2025년 11월 14일
편집: Voss 2025년 11월 25일
I've created an app using the app designer that creates a graph from user inputs with buttons for adding nodes and edges separately, updating the graph each time a node or edge is added. I want each node and edge to be clickable buttons which open a new dialogue in which additional data can be inputted and viewed in the form of a table or database. Any ideas on how I could achieve this?
The inputs occur like this currently in case it's helpful:
function NewNodeButtonPushed(app, event)
%get basic node data from user
nodetemp = inputdlg(["Node Name", "Latitude", "Longitude"], "Node Details");
%add code to start
nodeinp = addvars(cell2table(transpose(nodetemp),'VariableNames',{'Name','Latitude', ...
'Longitude'}), app.index, 'before', 1,'NewVariableNames',{'Code'});
%update graph with node data
app.NodeTable=[app.NodeTable; nodeinp];
%app.G=graph(app.EdgeTable,app.NodeTable);
app.G=addnode(app.G,nodeinp);
%display graph
plot(app.UIAxes,app.G,'NodeLabel',app.G.Nodes.Name,'EdgeLabel',app.G.Edges.Distance)%plot graph
app.UITable.Data = app.NodeTable;
app.index = app.index + 1; %increment node counter
end
  댓글 수: 3
Timothy
Timothy 2025년 11월 18일
Is it possible to create callbacks for the nodes and edge presses or is it better to switch off the hit test property and treat it like a regular axes?
Voss
Voss 2025년 11월 25일
편집: Voss 2025년 11월 25일

I haven't used a GraphPlot object before, but by my reading of the documentation, there doesn't appear to be a built-in way to distinguish which node/edge was clicked on.

If all else fails, yes, you should be able to turn HitTest off, capture the click by the axes, and do the necessary math to figure out what node or edge was clicked.

In fact, that's the approach I have always used, in professional software I've written in MATLAB, to do any mouse interaction with an object in an axes. I never use the object's ButtonDownFcn because I found it to be insufficient 20 years ago.

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

답변 (0개)

카테고리

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

제품


릴리스

R2025b

Community Treasure Hunt

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

Start Hunting!

Translated by