function outputStruct=DatareadPrn(filename)
% Reads the Agilent PNA *prn files and returns a struct with the Sparameters
% in the Real/Imag format
%
% Input
% filename  string with the file name. Example: data.prn
%
% Output    Struct with the Sparameters (complex number)
%           outputStruct.measurement    Measured S parameter
%           outputStruct.stimulus       Stimulus
%           outputStruct.data           S parameter data
%           outputStruct.stimulusLabel  Units of the Stimulus
%           outputStruct.dataLabel      Data of the Stimulus
%
%   v1, 5/03/2013
%
% function outputStruct=DatareadPrn(filename)

% Check that the file is in the correct format
if strfind(filename,'.prn'), 
else
    error('It is not a *.prn file'); 
end

% Read the line giving the units and the format
fid=fopen(filename);
% Read the measured quantity
tline = fgets(fid);
ind=strfind(tline,'"');
MeasQuantity=tline(ind(1)+1:ind(2)-1);
% Read the data label
tline = fgets(fid);
ind=strfind(tline,'"');
StimulusLabel=tline(ind(1)+1:ind(2)-1);
MeasQuantityLabel=tline(ind(3)+1:ind(4)-1);
fclose(fid);

% read the data
[stimulus,data]=...
    textread(filename,'%f, %f,','headerlines',2);

% Build the output struct
outputStruct.measurement=MeasQuantity;
outputStruct.stimulus=stimulus;
outputStruct.data=data;
outputStruct.stimulusLabel=StimulusLabel;
outputStruct.dataLabel=MeasQuantityLabel;


