function varargout = ScopeMathLoad(varargin);
% TEKLOAD Helper function for loading saved waveform data
%
% Usage:
% dataStruct = scopemathload(filename,fileformat);
%
% filename is the full name of the file you want to load.
% fileformat is a string containing the format the file was save as:
% {'MATLAB','MATHCAD','TXT','CSV'}
%
% Return Values:
% dataStruct is a structure that contains the waveform data and the time:
% dataStruct.Waveform and dataStruct.Time
%
% Example:
% data = tekload('CalibrationCurveMATLAB.DAT','MATLAB');
% plot(data.Time,data.Waveform);
%
% Notes: If the user did not save the waveform with the Waveform Detail:
% Include waveform scale factors checked then they will have an empty hdr file.
%
% end Notes
switch nargin
case 0
[filename, pathname] = uigetfile(fullfile(matlabroot,'Work','*.dat'));
if filename==0
% User hit cancel
return;
end;
[pathname,basefile,ext,versn]=fileparts(filename);
fileformat = 'MATLAB';
case 1
filename = varargin{1};
[pathname,basefile,ext,versn]=fileparts(filename);
fileformat = 'MATLAB';
case 2
filename = varargin{1};
[pathname,basefile,ext,versn]=fileparts(filename);
fileformat = varargin{2};
otherwise
help TekLoad
end;
if isempty(pathname)
error(['File does not exist ',filename, '. Check your path.']);
end;
Datafilename = [pathname,'\',basefile,ext];
[pathstr,basefile,ext,versn]=fileparts(Datafilename);
Headerfilename = fullfile(pathname,[basefile,'.hdr']);
if strcmpi(fileformat(1),'M')|strcmpi(fileformat(1),'T')
% Both MATLAB, TXT and Mathcad formats are the same.
data = loadMATLABFormat(Datafilename,Headerfilename);
elseif strcmpi(fileformat(1),'C')
data = loadCSVFormat(Datafilename);
else
error('Unknown file format');
end;
varargout{1} = data;
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
function data = loadMATLABFormat(Datafilename,Headerfilename)
try
waveform = load(Datafilename);
catch
error(['Error reading file',Datafilename'.']);
end
try
header = dlmread(Headerfilename);
catch
warning(['Error reading file',Headerfilename,'. Time vector will be incorect.']);
header = [length(waveform), 1, 0, 0];
end
% header = Record Length, Sample Interval, Trigger Point, Trigger Time.
data.Time = ([0:header(1)-1]'-header(3))*header(2);
data.Waveform = waveform(:);
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
function data = loadCSVFormat(Datafilename)
try
fid = fopen(Datafilename,'r');
str= fgetl(fid);
fclose(fid);
fid = fopen(Datafilename,'r');
idx = findstr(str,',');
if length(idx) ==4; %then the data was saved with waveform info.
for c=1:4
str = fgetl(fid);
idx = findstr(str,',');
time(c) = str2num(str(idx(3):idx(4)-1));
data(c) = str2num(str(idx(4):end));
end;
str = fgetl(fid);
while str~=-1
idx = findstr(str,',');
time(end+1) = str2num(str(idx(3):idx(4)-1));
data(end+1) = str2num(str(idx(4):end));
str = fgetl(fid);
end;
else
str = fgetl(fid);
while str~=-1
idx = findstr(str,',');
time(end+1) = str2num(str(idx(3):idx(4)-1));
data(end+1) = str2num(str(idx(4):end));
str = fgetl(fid);
end;
end;
catch
if fid
fclose(fid);
end;
error(['Error reading file',Datafilename,'.']);
end
% header = Record Length, Sample Interval, Trigger Point, Trigger Time.
data.Waveform = data(:);
data.Time = time(:);