1

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?

jblakeley
  • 816
  • 1
  • 10
  • 20

2 Answers2

0

You gonna need to load file first then use that variable

%Considering sparse.mat is in same directory as script    
load sparse %.mat is not not needed
%This will load the variables from graph lets say it had variable as data stored in it 
G = graph(double(data));
D=degree(G)

I think graph is not a function to plot for info on plotting functions visit http://in.mathworks.com/help/matlab/ref/plot.html

Rushikesh Tade
  • 473
  • 4
  • 7
0

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.

rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • @jblakeley - my pleasure :) If you no longer need help, please consider accepting my answer. That can be done by clicking on the checkmark icon, at the top of my post to the left below the up and down voting arrows. Good luck! – rayryeng Sep 30 '15 at 18:53