function [header,variables,data] = rawspice5(filename)
%rawspice5; [header,variables,data]=rawspice5('freq.raw')
%A function that reads spice3f5 "raw" files line-by-line
%and outputs strings and numerics compatible with MATLAB v5.
%Input: The text string containing the full path for the raw file.
%Outputs: [header,variables,data]. header is a cell array containing
%the contents of the first six lines of every raw file--each title
%(e.g. Title:) is removed (leaving, e.g. Bridge-T Circuit).
%variables is a cell array of strings containing the spice variable
%number, variable names (e.g. v(1) or sweep) and the units
%(e.g. frequency, voltage). Note: for MATLAB use, the variable
%numbers must be incremented by one. data is a numeric array with
%the data in columns corresponding to each of the variables
%(1 to #variables).
%Read the file line by line
fid = fopen(filename, 'rt');
n=1;
while feof(fid) == 0
line = fgetl(fid);
s{n}=line;
n = n+1;
end
n=n-1;
%Strip out header information--first 6 lines
for k=1:6
locs=findstr(s{k},':');
[nl n2]=size(s{k});
header{k}=s{k}(locs(1)+2:n2);
end
%Identify variables and variable types
%header{5} is the number of variables
nvars=str2num(header{5});
for k=8:8+nvars-1 %variable info begins on line 8
[nl n2]=size(s{k});
variables{k-7}=s{k}(2:n2);
end
npts=str2num(header{6});
line=nvars+9; %line where data starts
if strncmp(header{4},'complex',4) %identify data type
for k=1:npts
for k2=1:nvars
tabs=findstr(s{line},char(9)); %find tab location
[nl n2]=size(s{line});
comma=findstr(s{line},','); %find comma location
data(k,k2)=str2num(s{line}(tabs(1)+1:comma(1)-1))+i*str2num(s{line}(comma(1)+1:n2));
line=line+1;
end
line=line+1; %there's a blank line between data sets
end
% else, if data isn't complex...
else
for k=1:npts
for k2=1:nvars
tabs=findstr(s{line},char(9)); %find tab location
[nl n2]=size(s{line});
data(k,k2)=str2num(s{line}(tabs(1)+1:n2));
line=line+1;
end
line=line+1; %there's a blank line between data sets
end
end %end if
fclose(fid);