Question on reading datafile in matlab

1 view (last 30 days)
jana
jana on 19 Oct 2013
Commented: Cedric on 30 Oct 2013
I have a data file that looks like this:
SECTION Graph
Nodes 10
Edges 5
E 1 2 10
E 2 4 2
E 4 3 1
E 9 10 9
E 4 10 6
E 1 2 10
SECTION Terminals
Terminals 5
TP 1 3
TP 2 3
TP 3 5
TP 4 7
TP 8 9
END
EOF
means there is an edge between 1 and 2 and have a cost 10 and Terminals mean that node 13 have cost 3.I would like to read this data from my data file and create an adjacency matrix G using this data where the G(i,j) is the cost of edge(i,j) and it is -1 if there is no edge and a matrix P where P(i) is the cost of the i node and zero if no cost. Please Help example:
G = [0 10 0 0 0 0 0 0 0 0;
10 0 0 2 0 0 0 0 0 0;
0 0 0 1 0 0 0 0 0 0;
0 0 1 0 0 0 0 0 0 6 ;
0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 10;
0 0 0 0 6 0 0 0 10 0;]
P = [3 3 5 7 0 0 0 9 0 0]
  3 Comments
jana
jana on 19 Oct 2013
Edited: jana on 19 Oct 2013
I've created the matrix G, but I am having trouble with reading Terminals part:
function G = adjacencyMatrix()
filename = 'myFile.txt';
delimiterIn = ' ';
startRow = 1;
startCol = 1;
M = dlmread(filename,delimiterIn,startRow,startCol);
numNodes = M(1,1);
G = 0*ones(numNodes);
node1 = M(3:end,1);
node2 = M(3:end,2);
cost = M(3:end,3);
for k = 1:numel(node1)
G(node1(k),node2(k)) = cost(k);
G(node2(k),node1(k)) = cost(k);
end
end
Cedric
Cedric on 19 Oct 2013
Edited: Cedric on 19 Oct 2013
Ok, see my solution. Your file doesn't have a regular structure, so you cannot read it using DLMREAD. This leaves you basically with two approaches. The standard one based on FGETL/FSCANF, which is what you would be expected to produce for a homework I guess (which I can therefore not fully write for you), and the second based on pattern matching. I chose the latter, so you have something which works if it is not for a homework.
Let me know if you want/need to implement the standard approach and we can discuss it. Basically you would have to FOPEN the file, skip the first line with FGETL, read the second with FGETL and extract the number of nodes, same for the 3rd line (number of edges), and then read lines until it doesn't start with character 'E' (you could use the number of edges as well and implement a FOR loop, but not with the example that you provided, because there is a mismatch between Edges=5 and the six lines of edges). You would then proceed the same way for terminals, skipping a line with FGETL, reading the next and extracting the number of terminals, and then reading the block of terminals line by line.

Sign in to comment.

Accepted Answer

Cedric
Cedric on 19 Oct 2013
Edited: Cedric on 19 Oct 2013
Here is a start assuming that it is not for a homework (in the sense that a REGEXP-based approach would probably not be accepted as a homework solution).
% - Read file.
content = fileread( 'myFile.txt' ) ;
% - Extract graph information.
pattern = 'Nodes\s+(?<nodes>\d+)\s+Edges\s+(?<edges>\d+)\s+(?<data>.*)SEC' ;
graph = regexp( content, pattern, 'names' ) ;
graph.data = textscan( graph.data, '%*s %f %f %f' ) ;
% - Extract terminals information.
pattern = 'Terminals\s+(?<terminals>\d+)\s+(?<data>.*)END' ;
terminals = regexp( content, pattern, 'names' ) ;
terminals.data = textscan( terminals.data, '%*s %f %f' ) ;
Once this is executed, observe the content of structs graph and terminals:
>> graph
graph =
nodes: '10'
edges: '5'
data: {[6x1 double] [6x1 double] [6x1 double]}
>> terminals
terminals =
terminals: '5'
data: {[5x1 double] [5x1 double]}
and see how you can exploit it to build G and P.
EDIT: you might want to convert graph.nodes, graph.edges, and terminals.terminals to double using STR2DOUBLE, e.g.
graph.nodes = str2double( graph.nodes ) ;
  7 Comments
jana
jana on 30 Oct 2013
Sorry for the late reply. I've a lot of nodes, so I'll be using the sparse function. Thank you so much for helping me out here. I learnt a lot!

Sign in to comment.

More Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!