# ReadFiles

## PURPOSE

% Read in multiple ASCII .TXT Files

## SYNOPSIS

This is a script file.

## DESCRIPTION

    % Read in multiple ASCII .TXT Files
     This program reads in multiple ASCII text files into a single structure.

## CROSS-REFERENCE INFORMATION

This function calls:

- tfile tfile reads the data from an RDI ASCII output file and puts the

This function is called by:

## SOURCE CODE

    0001 %% Read in multiple ASCII .TXT Files
    0002 % This program reads in multiple ASCII text files into a single structure.
    0003
    0004 % August 5, 2008
    0005 % Jon Czuba
    0006 % USGS - Illinois Water Science Center
    0007
    0008 %% Determine Files to Process
    0009 % Prompt user for directory containing files
    0010 zPathName = uigetdir;
    0011 Files = dir(zPathName);
    0012 allFiles = {Files.name};
    0013 filefind=strfind(allFiles,'ASC.TXT')';
    0014 filesidx=nan(size(filefind,1),1);
    0015 for i=1:size(filefind,1)
    0016     filesidx(i,1)=size(filefind{i},1);
    0017 end
    0018 filesidx=find(filesidx>0);
    0019 files=allFiles(filesidx);
    0020
    0021 % Allow user to select which files are to be processed
    0022 selection = listdlg('ListSize',[300 300],'ListString', files);
    0023 zFileName = files(selection);
    0024
    0025 % Determine number of files to be processed
    0026 if  isa(zFileName,'cell')
    0027     z=size(zFileName,2);
    0028     zFileName=sort(zFileName);
    0029 else
    0030     z=1;
    0031     zFileName={zFileName}
    0032 end
    0033
    0034 %% Read in Selected Files
    0035 % Initialize row counter
    0036 j=0;
    0037 st=['A'; 'B'; 'C'; 'D'; 'E'; 'F'];
    0038 % Begin master loop
    0039 for zi=1:z
    0040     % Open txt data file
    0041     if  isa(zFileName,'cell')
    0042         fileName=strcat(zPathName,'\',zFileName(zi));
    0043         fileName=char(fileName);
    0044     else
    0045         fileName=strcat(zPathName,zFileName);
    0046     end
    0047
    0048     % screenData 0 leaves bad data as -32768, 1 converts to NaN
    0049     screenData=1;
    0050
    0051     % tfile reads the data from an RDI ASCII output file and puts the
    0052     % data in a Matlab data structure with major groups of:
    0053     % Sup - supporing data
    0054     % Wat - water data
    0055     % Nav - navigation data including GPS.
    0056     % Sensor - Sensor data
    0057     % Q - discharge related data
    0058     ignoreBS = 0;
    0059     [A(zi)]=tfile(fileName,screenData,ignoreBS);
    0060
    0061 end
    0062
    0063 %% Save data returned by tfile to .mat with same prefix as ASCII
    0064 idx=strfind(zFileName,'_');
    0065 namecut=zFileName{1}(1:idx{1}(1)-1);
    0066 numcut1=zFileName{1}(idx{1}(3)-2:idx{1}(3)-1);
    0067 numcut2=zFileName{z}(idx{z}(3)-2:idx{z}(3)-1);
    0068 savefile=strcat(namecut,'_',numcut1,'_',numcut2,'.mat');
    0069 save(savefile, 'A','z')
    0070
    0071 %% Clear Unwanted Variables
    0072
    0073 clear Files allFiles fileName filefind files filesidx i j screenData selection...
    0074     st zPathName zi idx namecut numcut1 numcut2