MATLAB Answers

Digraph With Node Labels From CSV Data

8 views (last 30 days)
I am trying to do some spectral clustering on a digraph, specifically, the digraph corresponding to C Elegans Adult Male, using the edge list available at I downloaded the csv file as column vectors, VarName1 (categorical array of the start node of an edge), VarName2 (categorical array of the end node of the edge), VarName3 (double array of the weight of the edge), and VarName4 (categorical array of the type of connection, chemical or electrical). All are of size 5107 x 1. For the moment, I am ignoring VarName4. As an example, here are the first 10 entries of VarName1-3:
>> [VarName1(1:5) VarName2(1:5)]
ans =
5×2 categorical array
I1L I3
I1L I5
I1L I6
>> VarName3(1:5)
ans =
I would like to construct a digraph from this data where the nodes are labeled as I1L, I2L, I3, etc. I tried
>> G = digraph(VarName1, VarName2, VarName3)
which spits out
Error using matlab.internal.graph.constructFromEdgeList (line 98)
Node IDs must both be numeric or both be character vectors.
Error in digraph (line 273)
matlab.internal.graph.constructFromEdgeList( ...
As a next step, I extracted the unique node labels as
>> UniqueLabels = unique([unique(VarName1); unique(VarName2)])
which is a categorical array of size 526 x 1, and then tried
>> G = digraph(VarName1, VarName2, VarName3, UniqueLabels)
which spits out
Error using matlab.internal.graph.constructFromEdgeList (line 62)
Node information must be a cell array of character vectors, a table, or a number of nodes.
Error in digraph (line 273)
matlab.internal.graph.constructFromEdgeList( ...
I would very much appreciate any ideas/suggestions that you might be able to provide. Thank you.
-- Kamal

Accepted Answer

Raunak Gupta
Raunak Gupta on 4 Dec 2019
From the error message I can understand that you are using MATLAB R2018b or earlier release. From R2019a onwards digraph supports categorical arrays as input to source nodes and target nodes vector. So, you may try updating the MATLAB with latest release.
If you are unable to do it straightaway, I suggest converting the categorical array to cell arrays as they are supported in release R2018b and earlier ones. You may find cellstr useful for converting categorical array to cell array. The below command should work.
G = digraph(cellstr(VarName1), cellstr(VarName2), VarName3);
You may find the changes in the Release Notes.
  1 Comment
Kamal Premaratne
Kamal Premaratne on 4 Dec 2019
Thank you very much for taking the time to respond to my query. I did in fact update to 2019a and it did work as you describe. I had actually tried your cellstr solution with the older 2017 version I had, but it did not work. But, all is well, because 2019a worked. Thanks again.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!