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).
content = fileread( 'myFile.txt' ) ;
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' ) ;
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 ) ;
Best Answer