function [net netLabels] = makeNet(data)
%========================================
% Construct the empty network matrix 'net'
% and associated cell array 'netLabels'
% Rows of 'net' = STARTnodes
% Columns of 'net' = ENDnodes
% i.e. net(startNodes, endNodes)
%========================================
%----------------------------------------
% Get a list of all the startNodes and endNodes in the data set, and put
% into cell array 'netLabels', to act as a reference for matrix 'net'
% showing which rows / columns of 'net' correspond to which nodes
nodes_withDups = {data.startNode data.endNode}; %Cell array of ALL startNodes and endNodes in 'data'
nodes = unique(nodes_withDups); %As above but duplicates removed
if isempty(nodes)
errordlg('makeNet could not find any data in variable ''data''')
net = [];
netLabels = {};
return;
end
% Get how many
noOfNodes = length(nodes);
% Initialise 'net' matrix with 0s, size = noOfNodes
net = zeros(noOfNodes);
% Initialise 'netLabels' cell array for storing node labels, size =
% noOfNodes
netLabels = cell(1,noOfNodes);
%Enter the row / column headings in netLabels (the nodes)
for cellCounter = 1:noOfNodes
netLabels{1,cellCounter} = nodes{cellCounter};
end
%----------------------------------------
%========================================
% Fill in the net with data values
%========================================
%[sizeData dummy] = size(data);
sizeData = length(data);
for dataCounter = 1:sizeData
%----------------------------------------
%---- Find index
% Find the index for the current startNode in the netLabels cell array
% (the 'coordinate').
startCoord = strmatch(data(dataCounter).startNode, netLabels, 'exact');
if isempty(startCoord)
error(['Error - cant find the startnode ' data(dataCounter).startNode ' in netLabels!'])
end
% Find the position of the current endNode in the netLabels cell array
endCoord = strmatch(data(dataCounter).endNode, netLabels, 'exact');
if isempty(endCoord)
error(['Error - cant find the endnode ' data(dataCounter).endNode ' in netLabels!'])
end
%----------------------------------------
%----------------------------------------
%---- Enter values
% If the current value in net is 0, and the new value
% (data.fibreDensity) is in range 1-3, enter the value
if (net(startCoord, endCoord) == 0) & (1 <= str2num(data(dataCounter).fibreDensity) <= 3)
net(startCoord, endCoord) = str2num(data(dataCounter).fibreDensity);
% If the new value already exists in table, warn that found duplicate
% data
elseif net(startCoord, endCoord) == str2num(data(dataCounter).fibreDensity)
% Uncomment for more information:
% disp(['Found duplicate data for connection ' data(dataCounter).startNode ' --> ' data(dataCounter).endNode ' (fibre density ' data(dataCounter).fibreDensity ')'])
% If the new value does not agree with already stored value, but is in range 1 - 3,
% warn that found conflicting data. Store an averaged value.
elseif (net(startCoord, endCoord) ~= str2num(data(dataCounter).fibreDensity) & (1 <= str2num(data(dataCounter).fibreDensity) <= 3))
average_density = mean( [net(startCoord, endCoord) str2num(data(dataCounter).fibreDensity)] );
fc_saveLog(['!! Found conflicting data values for ' data(dataCounter).startNode ' --> ' data(dataCounter).endNode ' - already stored density of ' num2str(net(startCoord, endCoord)) ' but found a conflicting value of ' data(dataCounter).fibreDensity '! Have stored average value: ' num2str(average_density)]);
net(startCoord, endCoord) = average_density;
% Otherwise, something has gone wrong - it may be a density value outside
% range 1-3?
else
warning(['Something went wrong with a data value for ' data(dataCounter).startNode ' --> ' data(dataCounter).endNode ' , perhaps an unexpected fibre density - value was ' data(dataCounter).fibreDensity])
end
%----------------------------------------
end
%========================================
%========================================
% Set main diagonal to all 0s - connections
% between a node and itself should be 0
%========================================
sizeNet = length(net);
for deleteCounter = 1:sizeNet
if net(deleteCounter, deleteCounter) ~= 0
net(deleteCounter, deleteCounter) = 0;
%disp(['Deleted connection between node ' netLabels{deleteCounter} ' and itself'])
end
end
%========================================
%========================================
% Perform CLUSTERING on the net
%========================================
[net, netLabels] = clustering(net, netLabels);
%========================================
return