Filter löschen
Filter löschen

Reading multiple text files

2 Ansichten (letzte 30 Tage)
Saleem Sarwar
Saleem Sarwar am 5 Okt. 2015
Bearbeitet: per isakson am 7 Okt. 2015
I am very thankful to mathwork community to promptly answers the queries asked by me. I am very new to matlab, wrote my first code to compute runoff using antecedent Soil Moisture Method and it is working well.
The only problem that I am facing now, is to extract data from text files. I asked the same question previously but was not able to solve the problem. Now rephrasing the question. My input data is in the form of text files, each file having daily rainfall data of specific year. I simply need to translate information into single file having 2 columns (1st column to represent date, mm/dd/yyyy and 2nd column to represent rainfall. I have 50 text files of a gauging station and requires to create a single output file from all these text files. The input file is attached for your reference.
Thanks for your cooperation
Kind regards,

Antworten (2)

Walter Roberson
Walter Roberson am 5 Okt. 2015
You say that in the result, column 1 should represent the data and column 2 should represent the rainfall, but you have not indicated where the rainfall is coming from.
Is it possible that you want column 1 to represent the date and column 2 to represent the the data that you extracted from those 50 files? If so then what information would you like stored for the date? Is the year information part of the file name?
  5 Kommentare
Walter Roberson
Walter Roberson am 5 Okt. 2015
Yes, that can be done. It requires fixed-width columns, though. Looking I see that the decimal points are 9 characters apart, so the missing data (because the day does not exist) can be determined by a sequence of 9 spaces. Because of that the easiest way to handle the missing entries would probably to use regexprep(LinesCell, '\s{9}', ' X') and then to regexp() 'split' the result on '\s+', vertcat() the resulting cells to get a rectangular array of cells, then str2double() to get numeric values.
The year is probably most easily pulled out of the second line by using regexp() of '\d{4}'
Anyhow, it is my bedtime, so I will leave this for you (or someone else) to implement.
Saleem Sarwar
Saleem Sarwar am 6 Okt. 2015
Hi all, I am very new to the matlab. I am not too expert to write a code for reading multiple text files and yet I have to do this for my assignment. Can any body please help me in this regard? I simply need to translate information into single file having 2 columns (1st column to represent date, mm/dd/yyyy and 2nd column to represent rainfall. The output file format is shown below Day Rainfall(mm)from input 50 text files( file attached for reference)

Melden Sie sich an, um zu kommentieren.


per isakson
per isakson am 6 Okt. 2015
Bearbeitet: per isakson am 7 Okt. 2015
Try cssm() it outputs to the screen
01/01/2013 0.0
01/02/2013 0.0
...
02/27/2013 14.6
02/28/2013 TR
03/01/2013 0.0
03/02/2013 0.0
...
12/30/2013 0.0
12/31/2013 0.0
where
function cssm()
filespec = 'h:\m\cssm\JHMRRR13.TXT';
fid = fopen( filespec );
hd = textscan( fid, '%s', 5, 'Delimiter', '\n' );
cac = textscan( fid, ['%f',repmat('%9s',[1,12])], 'WhiteSpace','' );
fclose( fid );
yy = regexp( hd{1}{2}, '\d{4}\s*$', 'match', 'once' );
y = str2double(yy);
d = cac{1};
for mm = 1 : 12
data = cac{mm+1};
for jj = 1 : length( data )
if not( strcmp( data{jj}, ' ' ) )
str = datestr( datenum([ y, mm, d(jj) ]), 'mm/dd/yyyy' );
fprintf( '%s%s\n', str, data{jj} )
end
end
end
end
&nbsp
Following Walter's advice and use '%9c' (see comment below)
function cssm()
filespec = 'h:\m\cssm\JHMRRR13.TXT';
fid = fopen( filespec );
hd = textscan( fid, '%s', 5, 'Delimiter', '\n' );
cac = textscan( fid, ['%f',repmat('%9c',[1,12])], 'WhiteSpace','' );
fclose( fid );
yy = regexp( hd{1}{2}, '\d{4}\s*$', 'match', 'once' );
y = str2double(yy);
d = cac{1};
for mm = 1 : 12
data = cac{mm+1};
for jj = 1 : length( data )
if not( strcmp( data(jj,:), ' ' ) )
str = datestr( datenum([ y, mm, d(jj) ]), 'mm/dd/yyyy' );
fprintf( '%s%s\n', str, data(jj,:) )
end
end
end
end
&nbsp
So far one file. To read 50 files more information is needed. Remember my questions?
  5 Kommentare
per isakson
per isakson am 7 Okt. 2015
Bearbeitet: per isakson am 7 Okt. 2015
Walter, thanks for your comments
"Fixed width" &nbsp The Matlab documentation fails me, and I don't have the ten years of experience with C needed to get it :-(
A little experiment with R2013a
>> cac = textscan( ' 12', '%2s%2s', 'WhiteSpace','' )
cac =
{1x1 cell} {1x1 cell}
>> cac{:}
ans =
' '
ans =
'12'
>> cac = textscan( ' 12', '%2c%2c', 'WhiteSpace','' )
cac =
' ' '12'
>> cac = textscan( ' 12', '%2c%2c' )
cac =
'12' [0x2 char]
Excerpt from R2015b documentation (same wording in R2013a)
When the field width operator is used with single characters (%c), textscan also reads delimiter, white-space, and end-of-line characters.
cac = textscan(' 12','%2c%2c') doesn't honor the documentation the way I interpret it.
Walter Roberson
Walter Roberson am 7 Okt. 2015
An even simpler example:
textscan(' 1','%c')
This would be expected to return cell with a 2 x 1 char, but instead it returns a cell with a 1 x 1 char. That is not what I would expect from the definition of %c

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by