Exposing graph layout functions

Plotting a graph G involves invoking plot(G), with an option to specify layout, e.g.,
G = graph(...);
plot(G,'Layout','force3');
Is there a way to expose the layout functions that assign coordinates to vertices without going through the machinery of creating a plot itself?
I know I can store the coordinate matrix by calling
Hgraphplot = plot(G, 'Layout','force3');
H.XData, H.YData
but this still generates a figure, axes, and all the associated steps that I'd like to bypass.
So is there a function of the sort
[XData, YData] = layoutcoordinates(G, 'Layout','force3');
whose only job is to generate coordinates?

4 Comments

toolbox/matlab/specgraph/+matlab/+graphics/+chart/+primitive/@GraphPlot/layoutforce3.p
but I have not been able to figure out yet how to invoke it.
Thanks Walter - I agree, I guess that's equivalent to calling
H.layout('force3')
in my example, which would require having a GraphPlot object H. I wonder if it can be constructed without the actual act of plotting - maybe that's a simpler task to do...
Hi,
Is graph the function you might be looking for?
Graph creates the object G above, but doesn't assign coordinates to nodes (vertices). I'm interested in the function that assigns coordinates to nodes ("layout" of the graph, to use the same language as MATLAB documentation).

Sign in to comment.

Answers (1)

Hi,
There is an undocumented way to get the coordinates without actually plotting the graph:
% Construct the graphics chart object without attaching to an axes or figure:
h = matlab.graphics.chart.primitive.GraphPlot('BasicGraph', ...
MLGraph(G), 'Layout', 'force');
% Get its XData and YData properties:
X = h.XData;
Y = h.YData;
The constructor here is an official interface. One can pass in the output of the undocumented method 'MLGraph' to this constructor, which will return an internal representation of the structure of G.
Since this is an internal and undocumented function, it is not officially supported and is subject to change in the future.
If this code were to stop working in the future, one could use "edit graph/plot" and step through the code to find the new internal syntax being used to generate GraphPlot object.
Hope this helps!
Thanks!

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Asked:

on 30 Oct 2020

Answered:

on 7 Jan 2021

Community Treasure Hunt

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

Start Hunting!