Reading a file and deleting lines of data that were written incorrectly
Show older comments
I am processing data from an instrument that periodically has lines (csv data file) where the data was written incorrectly. I have been deleting these by hand in a text editor but would like to automate the quality control.
I have tried to look at the fopen command, but can't determine from the help files how to simply delete one line out of the code.
I had so far produced some code as follows, that attempts to load the file, and then reads the errant line number from the error message:
while exit_i==1
try
exit_i=0;
raw_data=load(counts_file);
catch err
space_array = isspace(err.message);
space_index = find(space_array>0);
line_num = str2num(err.message(space_index(5):space_index(6)));
fileID = fopen(counts_file, 'rt+');
exit_i=1;
end
end
Now I would like to open the file and delete that line, and then repeat the loop until it clears through the try sequence and the file is completely cleaned up.
So, my question is, what combination of fopen etc. commands can accomplish this with information on line number?
Thanks, Ryan
4 Comments
Cedric
on 13 Jun 2014
Could you post one of these files and indicate which lines you want to remove?
Did dpb's answer solve your issue? If not, we really need a true sample, that you can attach to the post using the staple icon. Then copy-paste two lines as a comment that you format using code formatting (no problem if lines wrap), and indicate why one is correct and the other not. About your example, do you have a stream in your file which looks like what you mention (single line) or to you have one sequence per row like in what follows?
072752.24, 5,6,7,8,9,10
7,8,9,10
072753.24, 5,6,7,8,9,10
If you have a stream, how to identify sequences starts? Are these those floating point numbers? How to identify sequences length(s)? Are they constant (or should be)? If sequence starts are floats (implying that all other values in a sequence are integers), your example seems to indicate that there can be missing sequence starts. Could there also be missing integers? Or both? In such case, how would you eliminate cases where a sequence is truncated from its end, the next sequence is truncated from its start, and the two appended match the correct length for sequences?
dpb
on 16 Jun 2014
The solution I posted would not work for such a file; one would have to include the test on number of fields in a parsed line as well besides simply whether it was a well-formed csv record which is all the previous checks.
As you point out, in general with all the possibilities of an error it's quite complicated to clean up data.
Once upon a time many moons ago we got data back from the plant computers (nuclear commercial power stations) on punch paper tape. It was notoriously flaky for dropped punches and or reading. Had a terrible time developing code to try to salvage as much of the data as possible as at the time (late '60s/early '70s) it was our only way to get data other than by hand or the line printer out of the system.
Accepted Answer
More Answers (1)
Ryan
on 17 Jun 2014
1 Comment
dpb
on 17 Jun 2014
The one thing you might do would be to include your previous finding of the first failing line number; they you could use textscan or the like to slurp up the lines up to that point in one pass, saving the fgetl line-by-line stuff until need it. If the files are relatively small, the difference may not matter enough to bother.
Categories
Find more on Low-Level File I/O in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!