I want to represent a graph in MATLAB using information stored in a .mat file. The .mat file is an NxN square matrix. The goal is to be able to gather information on the graph (number of nodes, average degree, connected components, etc) using the matrix that represents edges between nodes.
I know Graph::createGraphFromMatrix exists but it is not supported in MATLAB.
I have tried variants of
G = graph(double('sparse.mat'));
D = degree(G);
But then I get errors like
Undefined function 'graph' for input arguments of type 'double'.
for whatever type I try. Does anyone know how to do this?
Firstly, you aren't properly loading the data into MATLAB. You need to use the load
function first to load in the data. You can then use the data in MATLAB once loaded in. It's as simple as:
load sparse.mat
However, I don't know what the graph variable is going to be called so you'll have to look at your workspace in that regard.
Now with your other problem, graph
is a function that was introduced as of R2015b. You are getting that error because your version of MATLAB is older than this and so graph
is not available with your distribution of MATLAB. In addition, the page you linked us to is part of the MuPAD interface. You can't run that in a normal MATLAB setting... as you can see from the warning on the page. The page you really want is this one: http://www.mathworks.com/help/matlab/ref/graph.html?searchHighlight=graph
Basically, you can't use graph
currently. One option is to upgrade your version of MATLAB. If this isn't an option, then other third party MATLAB libraries are possible. One of the best toolboxes available for download is directly from the MathWorks FileExchange website - in particular the grTheory
toolbox: http://www.mathworks.com/matlabcentral/fileexchange/4266-grtheory-graph-theory-toolbox. The function you're looking for is the grPlot
function.