How do I use adj2gephilab function for csv expot to gephi for network diagram representation

2 views (last 30 days)
Hi,
Can someone help me to assist how to use the function EdgeL=adj2gephilab(filename,ADJ,parameters)
I want to export my trade date between countries and show that in a Networt in gephi. What do I have to change in this code.
My Network structure from Matlab is stored in the variable G whcih consits of two matrices, labled "G.Edges" and "G.Nodes"
the Tratding data between the countries are stored inteh matrix "p_flow_res.F5(21).M"
the file is labeled "indicators.m"
I dont know how to use this plug in. It is all stord in the same folder.
I thougt I just chagne teh first function line with my variables like this:
function EdgeL=adj2gephilab(indicators,G,p_flow_res.F5(21).M)
But I get the error liek this:
>> adj2gephilab
File: adj2gephilab.m Line: 1 Column: 52
Invalid use of operator.
How to use this plugIn, which I got from https://de.mathworks.com/matlabcentral/fileexchange/51146-adj2gephilab Fabio Vanni
Thanks
code fo file adj2gephilab.m :
function EdgeL=adj2gephilab(filename,ADJ,parameters)
% Convert ana adjacency matrix of a graph to 2 spreadhseets csv files
% one for the edge table and the other for node table.
% The files _node.csv and _edge.csv have to be open
%in Gephi via Data Laboratory.
% INPUTS:
% filename: string for the prefix name of the two files .csv
% ADJ: the adjacency matrix
% parameters: vector as properties of the node to use as
% attributes of them.
% OUTPUTS:
% two csv spreadsheet files:
% filename_node.csv
% filename_edge.csv
% EdgeL = it returns the edge list corresponing to the
% adjacency matrix. (it can be saved to be open in Gephi too)
%
% The two files must be open in Gephi via the Data Laboratory
nodecsv=[filename,'_node.csv'];
edgecsv=[filename,'_edge.csv'];
n=size(ADJ,1); % square adjacency matrix
if nargin<3
parameters=ones(n,1);% all nodes have the same attributes
end
ps=parameters;
%% Node Table:
% header for node csv
fidN = fopen(nodecsv,'w','native','UTF-8'); % best format for gephi
fprintf(fidN,'%s\n','Id;Label;Attribute');
%
for i=2:n+1,
fprintf(fidN,'%s\n',[ num2str(i-1) ';"Node ' num2str(i-1) '"'...
';' num2str(ps(i-1))]);
end
fclose(fidN);
%% Edge Table
EdgeL=conv_EdgeList(ADJ);
S=EdgeL(:,1); % sources
T=EdgeL(:,2); % targets
W = EdgeL(:,3); % weights
fidE = fopen(edgecsv,'w','native','UTF-8');
% header for edge csv
fprintf(fidE,'%s\n','Source;Target;Label;Weight');
for i=2:length(S)+1,
fprintf(fidN,'%s\n',[ num2str(S(i-1)) ';' num2str(T(i-1)) ';'...
'"Edge from ' num2str(S(i-1)) ' to ' num2str(T(i-1)) '"' ';'...
num2str(W(i-1))]);
end
fclose(fidE);
%% Aux function
function EdgeL=conv_EdgeList(adj)
% convert adj matrix to edge list
n=size(adj,1); % number of nodes
edges=find(adj>0); % indices of all edges
n_e=length(edges);
EdgeL=zeros(n_e,3);
for e=1:n_e
[i,j]=ind2sub([n,n],edges(e)); % node indices of edge e
EdgeL(e,:)=[i j adj(i,j)];
end

Answers (1)

Jaynik
Jaynik on 10 Apr 2024
Hi Peter,
The error you are getting is likely because you are trying to pass "p_flow_res.F5(21).M" as a single parameter, but MATLAB is interpreting the '.' as an operator. Instead of changing the function directly, you should pass this as a separate variable and call the function in your code.
Here is how you might modify the function call in your script:
filename = 'indicators';
ADJ = G;
parameters = p_flow_res.F5(21).M;
EdgeL = adj2gephilab(filename, ADJ, parameters); % Call to the function
Note that, "adj2gephilab" should be on the MATLAB path.
  2 Comments
PETER
PETER on 10 Apr 2024
Hi, Thanks,
unfortunatelly the code is still not working:
____________
filename = 'indicators';
ADJ = G;
parameters = p_flow_res.F5(21).M;
EdgeL = adj2gephilab(filename, ADJ, parameters);
_____________
it gives these Errors:
________
Operator '>' is not supported for operands of type 'graph'.
Error in adj2gephilab>conv_EdgeList (line 61)
edges=find(adj>0); % indices of all edges
Error in adj2gephilab (line 40)
EdgeL=conv_EdgeList(ADJ);
Error in gephi_2st (line 4)
EdgeL = adj2gephilab(filename, ADJ, parameters);
__________
would it be acceptable for you to make a online zoom?
Jaynik
Jaynik on 10 Apr 2024
Edited: Jaynik on 10 Apr 2024
As per the documentation, the function "adj2gephilab" expects an adjacency matrix as input but from the error it seems that it is receiving a graph object. You can use the "adjacency" function to obtain the adjacency matrix for graph "G".
ADJ = adjacency(G);
If "G" is weighted graph, use this:
ADJ = adjacency(G, 'weighted');
Refer the following for more info on "adjacency": https://www.mathworks.com/help/matlab/ref/graph.adjacency.html

Sign in to comment.

Categories

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

Products

Community Treasure Hunt

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

Start Hunting!