function [scope] = hpimport(file);

%HPIMPORT: Read Input from HP Benchlink Software
%
%This function reads oscilloscope data from a file created
%by the the HP Benchlink Software.  The file must be saved 
%in a tab-separated format(.prn file), such as is used in 
%Lotus files. This option may be chosen from the Waveform 
%Options menu in the HP Benchscope program.
%
%To import data into Matlab, set the current directory of
%Matlab to the same directory in which the data file is
%located, then call the hpimport command, passing the name
%of the file in single quotes.  The routine returns a
%subscripted data type, which is then used by the HPREAD
%routine to access and plot data from individual channels.
%The motivation for this separation is due to the time that
%is required to read data from the file; hence, data is read
%from the file once, but may be examined multiple times,
%without a file-read each time.  An example follows:
%
%    scope_data = hpimport('practice.prn');
%
%The scope data member, 'scope_data', contains all the 
%necessary data from the file and will be used by HPREAD.  
%If a new  file is saved by the HP Benchlink software, then 
%HPIMPORT must be executed again on the new file, 
%saving the data to a new data member:
%
%   scope2_data = hpimport('prac2.prn');
%
%The data from the new file is located in 'scope2_data' and 
%the data from the previous file is still located in 'scope_data'.
%
%SEE ALSO: HPREAD

%Author: J.P. Van't Hof
%Date of Creation: 6/11/98

[fid,message] = fopen(file,'r');
if(~isempty(message))
   disp(message);
   scope = [];
   return;
end;

%get the headers of the top of the file
for (i = 1:3);
   junk = fgetl(fid);
end;

%get the channel number from the file
line = fgetl(fid);
channels = sscanf(line,'%s');

%get rid of the junk in the file
junk = fgetl(fid);
junk = fgetl(fid);

%get the labels for the channels
line = fgetl(fid);
labels = sscanf(line,'%s');

%take the actual data and place into a N x 2
data = [];
while(~feof(fid))
   line = fgetl(fid);
   [S,count] = sscanf(line,'%f');
   S=S';
   data = [data;S];
end;

num = count / 2;

%take all of the channel and label info and place it into
%the data structure to be returned
%(allows for automatic plot labeling)
if(num >= 1)
   chan(1,:) = channels( 8:12);
   xlab(1,:) = labels( 7);
   ylab(1,:) = labels(14);
end
if(num >= 2)
   chan(2,:) = channels(32:36);
   xlab(2,:) = labels(21);
   ylab(2,:) = labels(28);
end
if(num >= 3)
   chan(3,:) = channels(56:60);
   xlab(3,:) = labels(35);
   ylab(3,:) = labels(42);

end
if(num >= 4)
   chan(4,:) = channels(80:84);
   xlab(4,:) = labels(49);
   ylab(4,:) = labels(56);
end

format1 = cellstr(char(chan))';
format2 = cellstr(char(xlab))';
format3 = cellstr(char(ylab))';
format = [format1;format2;format3];

%take the data, and format info (channels and labels)
%and place it into one data structure (object)
scope.data=data;
scope.format = format;
scope.num = num;

%tell everyone that it worked
disp('Import successful.');

%close the file
fclose(fid);