How can I delete header from a .txt file and save it?
4 views (last 30 days)
Show older comments
Javier Martinez
on 1 Jul 2016
Commented: Guillaume
on 1 Jul 2016
I have a text file like this one:
element, unit, refTime, validTime, (62.000000,-6.250000)
HTSGW, [m], 197901010000, 197901010000, 0.000
HTSGW, [m], 197901010000, 197901010300, 0.220
HTSGW, [m], 197901010000, 197901010600, 0.420
HTSGW, [m], 197901010000, 197901010900, 0.560
HTSGW, [m], 197901010000, 197901011200, 2.190
I just want to remove the header and then save the file in the same .txt file without the header.
I may use the next script, shown in other forums, however I cannot save the generated list.
fid = fopen(Text);
tline = fgetl(fid);
tline = fgetl(fid);
while ischar(tline)
disp(tline)
tline = fgetl(fid);
end
fclose(fid);
I wish someone could help me. Thanks.
4 Comments
Guillaume
on 1 Jul 2016
Ah yes, certainly it would be more efficient to do that outside of matlab with the OS tools (assuming there is one that does that, not sure about windows...).
Accepted Answer
Azzi Abdelmalek
on 1 Jul 2016
fid=fopen('file.txt')
s=textscan(fid,'%s%s%s%s%s','headerlines',1)
fclose(fid)
ss=[s{:}]
fid1=fopen('file.txt','w')
for k=1:size(ss,1)
fprintf(fid1,'%s%s%s%s%s\r\n',ss{k,:})
end
fclose(fid1)
3 Comments
Guillaume
on 1 Jul 2016
Why bother parsing each line into 5 different strings, thus slowing down the program. You actually don't care about the content of each line, so
s = textscan(fid, '%s', ...);
should be enough. Same for the fprintf call.
More Answers (1)
Guillaume
on 1 Jul 2016
Edited: Guillaume
on 1 Jul 2016
Note: when using fopen always specify the permissions ('r' or 'w' mostly with optional 't' for text mode).
Your code only display the text. You also need to open a file for writing if you want to save it.
Probably the easiest way to do it all would be:
filecontent = fileread('somefile');
newcontent = regexp(filecontent, '(?<=[^\n\r].*[\n\r]+).*', 'match'); %keep everything but first line (defined as any number of characters but newline or carriage return followed by any number of newlines or carriage returns
fid = fopen('newfile', 'w');
fprintf(fid, '%s', newcontent);
fclose(fid);
2 Comments
Guillaume
on 1 Jul 2016
D'oh! either add , 'once' after the 'match' in the regexp call. or simply dereference the cell as it should only have one element:
fprintf(fid, '%s', newcontent{1});
Adding 'once' would be more correct.
See Also
Categories
Find more on String Parsing 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!