% IO_readTraffFile
%
% Usage: [traff_trafficMatrix, errmsg, errorFlag] =
% IO_readTraffFile(fullpathname)
%
% Abstract: This function reads a Traffic File (*.traff) from the
% filename selected by the user in the pathname selected by the user.
% A Traffic File (*.traff)) contains the Traffic Matrix, that
% is, the table of the traffic demands for all the possible node
% pairs.
%
% In the whole file the columns (fields) are separated by TABS and the rows
% by RETURNS. Each field column is represented according to C language
% FORMAT. The FORMAT is a string containing C language conversion specifications.
%A conversion specification controls the notation, alignment, significant digits,
%field width, and other aspects of output format. The format string can
%contain escape characters to represent nonprinting characters such as
%newline characters and tabs. Conversion specifications begin with
%the % character and contain these optional and required elements:
% Flags (optional)
% Width and precision fields (optional)
% A subtype specifier (optional)
% .Conversion character (required)
%
% Arguments:
% o In:
% . fullpathname: String of the pathname of the Traffic File (*.traff)
% to save, incluiding the filename: [pathname filename]
%
% o Out:
% traff_trafficMatrix(NxN): Average traffic flow offered between node pairs. The
% Traffic Matrix is a two-dimensional matrix with N (N: number of nodes) rows
% and N columns. An entry(s,d) means the average traffic flow from node 's'
% to node 'd', measured as the number of bits per second.
%
% . errmsg: Error message. If the string is valid (errorFlag = 0),
% errmsg='No Error'; otherwise, errmsg indicates the error in the string.
%
% . errorFlag: If the phys string is valid, errorFlag = 0; otherwise
% errorFlag = -1.
%
% Ramon Aparicio Pardo,2007
function [traff_trafficMatrix, errmsg, errorFlag] = IO_readTraffFile(fullpathname)
%We open the file to read our 'Traffic File (*.traff)'
% [filename, pathname] = uigetfile({'*.traff','Traffic File (*.traff)'},'Open Traffic File','Data\Traffics\');
% fullpathname=[pathname filename];
fid = fopen(fullpathname);
traff_trafficMatrix=[];
%0.Information File*********************************************************
try
infoReadFile = textscan(fid, '%f', 1, 'commentStyle', '//', 'returnOnError', 0);
catch
fclose(fid);
errmsg =['Format Error: Fail on reading Nr Nodes'];
errorFlag = -1;
return
end
%We get the fields from the cell obtained with textscan
numberOfNodes = cell2mat(infoReadFile(1));
%At the beginning, everything is right
errmsg='No error';
errorFlag = 0;
%We check the format rightness of the Information File Fields.
if ((abs(numberOfNodes)-abs(fix(numberOfNodes)) ~= 0) | (numberOfNodes <= 0)),
traff_trafficMatrix=[];
errmsg =['Format Error: The field "Number of Nodes" must be a positive integer'];
errorFlag = -1;
return
end
%1.Traffic Table - Traffic**************************************************
%We define the format of the Traffic information in the file
formatInput='';
for i=1:numberOfNodes,
formatInput=[formatInput '%f'];
end
try
infoTraffic = textscan(fid, formatInput, numberOfNodes, 'commentStyle', '//', 'returnOnError', 0);
catch
errmsg =['Format Error: Mismatch between the expected file format and the found file format.'];
errorFlag = -1;
return
end
%We get the fields from the cell obtained with textscan
traff_trafficMatrix = cell2mat(infoTraffic);
%2.End Of File
EndOfFile = textscan(fid, '%s', 1, 'commentStyle', '//', 'returnOnError', 0);
EndOfFile=cell2mat(EndOfFile{1,1});
if length(EndOfFile)==50,
if any(EndOfFile~='****************END_OF_FILE_.TRAFF****************'),
fclose(fid);
errmsg =['Format Error: Mismatch between the expected file format and the found file format.'];
errorFlag = -1;
return
end
else
fclose(fid);
errmsg =['Format Error: Mismatch between the expected file format and the found file format.'];
errorFlag = -1;
return
end
fclose(fid);
%%%%%%%%%%%We are going to validate the read fields%%%%%%%%%%%%%%%%%%%%%%%%%
%We check the format rightness of the Traffic Table Fields.
for i=1:numberOfNodes,
for j=1:numberOfNodes,
if (traff_trafficMatrix(i,j) < 0),
traff_trafficMatrix=[];
errmsg=['Field Error: The element of the Traffic Table in the position (',...
num2str(i),',', num2str(j),') must be a positive value '];
errorFlag = -1
return
end
end
end