Parsing a Large Text File into Sections
조회 수: 3 (최근 30일)
이전 댓글 표시
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
채택된 답변
Sven
2012년 8월 18일
편집: Sven
2012년 8월 18일
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
추가 답변 (2개)
Amanda
2012년 8월 18일
편집: Amanda
2012년 8월 18일
댓글 수: 1
Sven
2012년 8월 18일
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
2012년 8월 18일
댓글 수: 2
Sven
2012년 8월 18일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Text Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!