Posted:9/27/2013 7:35PM

Matlab Tektronix Oscilloscope CSV File Loader

Mike Mclain discusses how to use Matlab to load a Tektronix Oscilloscope CSV File

Preface:

Because my academic research involved the empirical acquisition of laboratory measurements (generally through the utilization of a serial remote interface with a Tektronix TPS2024 oscilloscope) I frequently had massive amounts of Tektronix encoded CSV data that needed to be imported into Matlab for further analysis.

Now, for those of you who are not familiar with the Tektronix encoded CSV file format, here is a small example:

Record Length,2.500000e+03,,  -0.250000000000,   2.00000,
Sample Interval,2.000000e-04,,  -0.249800000000,   2.00000,
Trigger Point,1.250000000000e+03,,  -0.249600000000,   1.92000,
,,,  -0.249400000000,   1.84000,
,,,  -0.249200000000,   1.76000,
,,,  -0.249000000000,   1.76000,
Source,CH1,,  -0.248800000000,   1.68000,
Vertical Units,V,,  -0.248600000000,   1.60000,
Vertical Scale,2.000000e+00,,  -0.248400000000,   1.52000,
Vertical Offset,0.000000e+00,,  -0.248200000000,   1.52000,
Horizontal Units,s,,  -0.248000000000,   1.44000,
Horizontal Scale,5.000000e-02,,  -0.247800000000,   1.36000,
Pt Fmt,Y,,  -0.247600000000,   1.28000,
Yzero,0.000000e+00,,  -0.247400000000,   1.20000,
Probe Atten,1.000000e+01,,  -0.247200000000,   1.20000,
Firmware Version,FV:v10.21,,  -0.247000000000,   1.12000,
,,,-00.246800000000,   1.04000,
,,,-00.246600000000,   0.96000,
,,,-00.246400000000,   0.88000,
,,,-00.246200000000,   0.88000,
,,,-00.246000000000,   0.80000,
,,,-00.245800000000,   0.72000,
,,,-00.245600000000,   0.64000,
,,,-00.245400000000,   0.56000,
,,,-00.245200000000,   0.56000

Likewise, while I normally utilized a custom Python script to preprocess all of my laboratory acquisitions into a simplistic numerical CSV file format that i could easily import into Matlab (or in other words I seldom utilized the default Tektronix TPS2024 file format); however, occasionally the need would arise to load the original Tektronix encoded format and depending upon the version of Matlab utilized and the desired end objective (an example being trying to get the Tektronix channel information in to an isolated array structure) this task can be relatively trivial or extremely difficult to achieve using inline Matlab commands.

Nevertheless, while I am sure that some folks in the peanut gallery might recommend other ways of achieving this task within Matlab (and feel free to do so); however, i found that the following method seems to work pretty well for a variety of Matlab task.

Example Usage in Matlab:

[data,header]=LoadTexCSV(F0000CH1.CSV)

The Matlab Function:

%*************************************************************************
%*This function will load a Tektronix encoded CSV File                   *
%*************************************************************************

% Open the File
fileID = fopen(filename);

% Create a storage elements to hold channel information
output=[];
% Create a storage element to hold scope information

% Get the first line
csvline = fgetl(fileID);

% a storage element to keep track of the current line within the file
linenumber=1;

% a constant value that determines when to stop processing header
% information
% define constant delimiter locations
constant_line_index_time=4;
constant_line_index_value=5;

% if the line has a character process the line
while ischar(csvline)
% Segment the line into a array based upon the comma delimiter

% see if we are working within the header

% check and see if the header is good
if strcmp(parameter,'')~=1
% Attempt to Convert value to a number
% check and see if the conversion worked
if isnan(value)
% if the conversion did not work, revert back to a string
end

% see if this is the first time executing
if linenumber==1
% if it is then overwrite header
else
end
end
end

% Take the good extracted Data and turn it into a numerical value
time=str2double(data(constant_line_index_time));
value=str2double(data(constant_line_index_value));

% see if this is the first time executing
if linenumber==1
% if it is then overwrite output
output=[time value];
else
% else augment output
output=[output; time value];
end

% Get the next line
csvline = fgetl(fileID);

% increment the line number
linenumber=linenumber+1;
end

% Close the File
fclose(fileID);
end

Enjoy!