Posted:9/27/2013 7:35PM
Mike Mclain discusses how to use Matlab to load a Tektronix Oscilloscope CSV File
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.
[data,header]=LoadTexCSV(F0000CH1.CSV)
%************************************************************************* %*This function will load a Tektronix encoded CSV File * %************************************************************************* function [ output,header] = LoadTexCSV(filename) % Open the File fileID = fopen(filename); % Create a storage elements to hold channel information output=[]; % Create a storage element to hold scope information header={}; % 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 constant_header_stops_after=16; % define constant delimiter locations constant_line_index_header_name=1; constant_line_index_header_value=2; 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 data=strread(csvline,'%s','delimiter',','); % see if we are working within the header if linenumber <= constant_header_stops_after % buffer the header item parameter=data(constant_line_index_header_name); % check and see if the header is good if strcmp(parameter,'')~=1 % Attempt to Convert value to a number value=str2double(data(constant_line_index_header_value)); % check and see if the conversion worked if isnan(value) % if the conversion did not work, revert back to a string value=data(constant_line_index_header_value); end % see if this is the first time executing if linenumber==1 % if it is then overwrite header header=[{parameter value}]; else % else augment header header=[header; {parameter value}]; 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!
By Mike Mclain