0
votes

I have a question: I have download 1 rainfall data int Text file *.txt it included texts, heading, bottom text, and data also some of spaces line between data. When I import the file into the Matlab, The matlab could not defined each cells included column, and rows (non delimiter)

I have did like, convert text file to Excel, and remove the cols and rows very easily and save with another files. But my data is up to 846000 data set ~ 24h x 30days x 12month x 20 years which combined many different files for each data. SO it will difficult to make manual converting like I did. My adviser told me that there are Matlab CODE could do it well. Does anyone can help me this problem?

The original: https://drive.google.com/file/d/0By5tEg03EXCpekNaemItMF85ZWs/edit?usp=sharing

1

1 Answers

1
votes

If you're on Mac or Linux I suggest converting these data files using the shell into a format Matlab will like rather than trying to make Matlab do it. This works on Windows too, but only if you have a unix-like shell installed such as MinGW, Cygwin or Git Bash.

For example, this converts the raw data section of the file you shared into CSV:

cat "$file" | sed 's:  *:,:g' | sed 's:^,::' | grep '^[0-9]' > "$file".csv

You could then loop through all your raw data files and combine them into a single CSV like this:

for file in *.txt; do
  cat "$file" | sed 's:  *:,:g' | sed 's:^,::' | grep '^[0-9]' >> all.csv
done

If you need to preserve, for example, which year and which weather station, you could get a little fancier with it and capture those values at the beginning of each file and turn them into columns on each line. Here's an example that grabs the year and weather station ID and inserts it as a column before each day.

for file in *.txt; do
  station="$(grep 'Station -' "$file" | sed 's: *Station - ::' | sed 's:   .*::' | uniq)"
  year="$(grep 'Water Year' "$file" | awk '{print $4}')"
  cat "$file" | sed 's:  *:,:g' | grep '^,[0-9]' |\
    sed "s/^,/$station,$year,/" >> all.csv
done