No BSD License  

Highlights from
CoCoMac

image thumbnail
from CoCoMac by James Allen
Download CoCoMac.org cortical connectivity data, model it as a network, and simulate epileptic sprea

fc_readPajek(path)
function [net, netLabels, nodeCoords] = fc_readPajek(path)

%Function to read a pajek file and load data into main program

% If no argument supplied, we need to ask the user for a file to load.
if nargin == 0
    [pajek_name, pajek_path] = uigetfile('.net','Select source pajek file');
    pajekPath = [pajek_path pajek_name];
else
    pajekPath = path;
end

% If the user did not select a file..
if isequal(pajekPath,0)
    errordlg('Warning - no file selected')
    net = [];
    netLabels = [];
    nodeCoords = [];
    return;
else
    try
        pajekFile = fopen(pajekPath);
    catch
        errordlg(['Error - couldnt open pajek file: ' lasterr])
        net = [];
        netLabels = [];
        nodeCoords = [];
        return;
    end
    if pajekFile == 0
        errordlg(['Error - couldnt open pajek file: ' lasterr])
        net = [];
        netLabels = [];
        nodeCoords = [];
        return;
    end
end

% First work out if there are 3-D coords in the pajek file. Do this by
% looking at the first 5 items from textscan - if the 5th is a '2', there
% are no coords
firstFiveItems = textscan(pajekFile, '%s', 5);
areThereCoords = char(firstFiveItems{1}(5));
if areThereCoords(2) == '2'
    coordsPresent = 0
else
    coordsPresent = 1
end

% Reset textscan
fclose(pajekFile);
pajekFile = fopen(pajekPath);

% Read the first string, "*Vertices"
dummy = textscan(pajekFile, '%s', 1);

%Read how many vertices
noOfVertices = textscan(pajekFile, '%n', 1);

% A dummy blank space
dummy = textscan(pajekFile, '%s', 1);

% Some initialisations
netLabels = {};
nodeCoords = [];
net = zeros(noOfVertices{1});

% Read the name of the vertex (name of node), put into netLabels. The
% name is always surrounded by "quotes" - using %q removes quotes.
for vertexCounter = 1:noOfVertices{1}
    
    %Read the vertex number. Not needed.
    dummy = textscan(pajekFile, '%s', 1);
    
    temp = textscan(pajekFile, '%q', 1);
    netLabels{end+1} = char(temp{1}(:));

    %Read x, y and z coordinates into nodeCoords
    if coordsPresent
        coords = textscan(pajekFile, '%n %n %n', 3);
        nodeCoords(end+1, 1:3) = [coords{1} coords{2} coords{3}];
    else
        % A dummy blank space
        dummy = textscan(pajekFile, '%s', 1);
    end
end

% Read dummy string, "*Arcs"
dummy = textscan(pajekFile, '%s', 1);

% Read arc data
arcData = textscan(pajekFile, '%s %s %s'); %Note: used %s (string) because for some reason when using %n, textscan would NOT read to the end of the file - no idea why

startNode = arcData{1};
endNode = arcData{2};
density = arcData{3};
noOfArcs = length(startNode);

% read arc data into net
for arcCounter = 1:noOfArcs
    net(str2num(startNode{arcCounter}), str2num(endNode{arcCounter})) = str2num(density{arcCounter});
end

fclose(pajekFile);
fc_saveLog(['Loaded network data from: ' pajekPath]); % Log entry in GUI_log.txt

return

Contact us at files@mathworks.com