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!