0
votes

I've used neworkx to generate a random geometric graph on 50 nodes, and create a .dat file with some attributes of this network.

I need to access these as MATLAB variables. I read the file in as a data string using:

fid = fopen('mydata.dat','r')
data = textscan(fid, '%s')
fclose(fid)

The structure of the data file is as follows

conn = val
Adj = val ..... val
      .............
      val ......val
pos = 
[0.7910629988376467, 0.5523474928588686]
...
[0.6799716933198028, 0.6981655240935597]

i.e. conn is a number, Adj is (supposed to be) a 50 by 50 matrix and pos is a 50 by 2 matrix.

I can read conn, and Adj as MATLAB variables fine, but I'm having trouble reading pos. The first instance starts at data{1}{2508}, and is

data{1}{2508} 
>>> [0.7832623541518583,

How do I shoehorn this into a 50 by 2 (or 2 by 50) matrix?

To read the Adj I use

P = 50 %number of nodes
index = 5

for i=1:P
    for j = 1:P
        Adj(i,j) = str2double(data{1}(index + P*(i-1) +j))
    end
end

I thought something similar would work for pos, but with j = 1:2 and index = 2508 but I'm getting NaNs as the lines (fields?) of my .dat file aren't just values, they're of the form [val, or ,val]

1

1 Answers

1
votes

You can first delete all characters you don't want to have.

data = regexprep(data{1},'[\[\],]','');

After that, your loop should succeed. However, you can speed up your code by using array functions.

Find the occurance of pos

ind = find(strcmp(data,'pos')); # Should be 2506 in your case

After that, once you know that your array is 50x2 use:

pos = str2double(reshape(data(pos+2:end),2,50)')

Note, the +2 is for pos and =.