Parsing a Large Text File into Sections
3 views (last 30 days)
Show older comments
I have a large text file as below:
Run Lat Long Time
1 32 32 34
1 23 22 21
2 23 12 11
2 11 11 11
2 33 11 12
up to 10 runs etc.
So I'm trying to break up each section in the file: section 1, section 2, etc and write it to 10 different text files. File 1 will have data from Run 1. File 2 will have data from Run 2.
Thanks,
Amanda
0 Comments
Accepted Answer
Sven
on 18 Aug 2012
Edited: Sven
on 18 Aug 2012
Hi Amanda,
This should work for you. It just reads the input file one line at a time and prints that line to an output file. If it hits a new "section", it makes a new output file named by that section.
fidIn = fopen('inputFile.txt','r');
oldFirstChars = 'somethingtostart';
fidOut = [];
while 1
tline = fgetl(fidIn);
if ~ischar(tline), break, end % Handle the end of the input file
% Get the string up to the first space
newFirstChars = regexp(tline, '\d+','match','once');
% If it's a new "section", make a new file
if ~strcmp(oldFirstChars, newFirstChars)
if ~isempty(fidOut)
% Close the old file first
fclose(fidOut);
end
fidOut = fopen(['outputFile' newFirstChars '.txt'],'w');
oldFirstChars = newFirstChars;
end
% Just print out the line that we just read to the output file
fprintf(fidOut, '%s\r\n',tline);
end
% Clean up any open files
fclose(fidIn);
fclose(fidOut);
0 Comments
More Answers (2)
Amanda
on 18 Aug 2012
Edited: Amanda
on 18 Aug 2012
1 Comment
Sven
on 18 Aug 2012
Hi Amanda, this line is just to extract the first 1 (or 2 or 3 depending on how many digits in the section number) characters from the string. Do you have a space character after your section number, or a tab character?
There will definitely be a more robust way than I wrote (I just search for the first "space" character). Perhaps a regexp such as:
newFirstChars = regexp(tline, '\d+','match','once')
Amanda
on 18 Aug 2012
2 Comments
Sven
on 18 Aug 2012
Hi Amanda, I just tested the script, and it does exactly that. I've made a change or two now to fix two little bugs:
1. It now works even if the first line is the headers (and not a section)
2. It now puts a newline/carriage return rather than just a newline between lines (so that it shows up on different rows in notepad)
I've edited my first answer with these changes. (by the way, you can hit "comment" rather than "answer" if you want to comment on someone's answer).
Thanks, Sven.
See Also
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!