## Copyright (C) 2005, Gerald Ebberink ## This program is free software; you can redistribute it and/or modify ## ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA ## reads the csv's made by tektronix osciloscopes ## This function demands that the osciloscope is asked to "save all" and that there is a channel1 ## recoreded ## Since this read is slow, it also saves a octave bin file in the directory, so any subsequent ## reads are much faster. ## Useage: data = Tekread(datadir) ## datadir is a string which gives the relative directory which is to be read ## data is structure returned with several members ## data.WaveformX is the data of channel X ## data.Time is the time vector ## data.Index is a vector which has the same length as data.waveform1 and can be used to shift the figures a little ## Author: Gerald Ebberink ## Keywords: file I/O tektronics ## Last Change: may 2005 function data = Tekread(datadir) ## if the directory ends with a slash ## kill the slash, this is usefull later on if datadir(end)=='/' datadir=datadir(1:end-1); end; ## The filenames in the data dir are numbered by the osciloscope ## to get the right number the folowing is done lscommand = sprintf('ls "%s/"', datadir); filename = system(lscommand); fileprepend = filename(1:7); ## now let us see if we have read this data before ## and if we did read the data file instead of go into the slow process if (findstr(filename,'.dat')) loadcommand = sprintf('load %s/zdata.dat',datadir); eval(loadcommand); else % So it is the first time we read this data. % open first data file filename = sprintf('%s/%s1.csv',datadir,fileprepend); fid = fopen(filename,'rt'); %if the data file exist go on if fid > 0 str = fgetl(fid); %get the first line dat = []; %and reset dat while str~=-1 idx = findstr(str,','); if findstr(str,'Vertical Scale') % search for the Vertical scale and put it in the data set data.VerticalScale(1) = str2num(str(idx(1)+1:idx(2)-1)); end; if findstr(str,'Vertical Units') % search for the unit and put it in the data set data.VerticalUnit(1) = str(idx(1)+1:idx(2)-1); end; if findstr(str,'Vertical Offset') % search for the Offset number and again put it in the data set data.VerticalOffset(1) = str2num(str(idx(1)+1:idx(2)-1)); end; if findstr(str,'Horizontal Scale') % search for the Horizontal scale data.HorizontalScale = str2num(str(idx(1)+1:idx(2)-1)); end; if findstr(str, 'Horizontal Units') % find the Horizontal Units data.HorizontalUnit = str(idx(1)+1:idx(2)-1); end; Time(end+1) = str2num(str(idx(3)+1:idx(4)-1)); % Put the time data in the time array dat(end+1) = str2num(str(idx(4)+1:end-1)); % Put the data in the dat array str = fgetl(fid); % get next line end; fclose(fid); data.Time=Time(2:end); % put the time data in the data structure data.Waveform1=dat(1:end); % put the waveform data in the data structure end; for c=2:4 % for the next 3 channels filename = sprintf('%s/%s%i.csv',datadir,fileprepend,c); fid = fopen(filename,'rt'); %open channel file if fid > 0 % if file exists str = fgetl(fid); %get first line dat=[]; % reset data structure while str~=-1 idx = findstr(str,','); if findstr(str,'Vertical Units') % search for the unit and put it in the data set data.VerticalUnit(c) = str(idx(1)+1:idx(2)-1); end; if findstr(str,'Vertical Offset') % search for the Offset number and again put it in the data set data.VerticalOffset(c) = str2num(str(idx(1)+1:idx(2)-1)); end; if findstr(str,'Vertical Scale') % search for the Vertical scale and put it in the data set data.VerticalScale(c) = str2num(str(idx(1)+1:idx(2)-1)); end; dat(end+1) = str2num(str(idx(4)+1:end-1)); str = fgetl(fid); end; switch (c) case (2) data.Waveform2=dat(1:end); % if channel 2 put data in channel 2 case (3) data.Waveform3=dat(1:end); % if channel 3 put data in channel 3 case (4) data.Waveform4=dat(1:end); % if channel 4 put data in channel 4 endswitch; end; end; % Make a sample index (this is usefull in other programs) data.Index=[1:1:size(data.Waveform1,2)]; %save data savecommand=sprintf('save %s/zdata.dat data',datadir); eval(savecommand); end;