How to only keep lines that start with a number?
Show older comments
If my code is something like
sdfjsjfsefjdsf
sdfsdfsdfsdfsdf
fsdfsdfsdfsdf
1232
12132
and only numbers beyond that..
I would like to modify my text file such that only the numerical data remains. How do I do that? Thanks.
2 Comments
Geoff Hayes
on 14 Jul 2016
Abu - is it just the first three lines or more? Can the lines with non-numerical data be anywhere in the list? Please clarify.
Answers (2)
Azzi Abdelmalek
on 14 Jul 2016
If filename.tx is your text file
fid=fopen('filename.txt')
l=fgetl(fid);
s={};
while ischar(l)
s{end+1,1}=l;
l=fgetl(fid);
end
fclose (fid)
out=regexp(s,'\<\d+\S+','match','once')
Sean de Wolski
on 14 Jul 2016
x = fileread('test.csv')
xc = strsplit(x,sprintf('\n')) % split to cellstr
ix = regexp(xc,'^\d') % match a digit at beginning of line
idxdigit = ~cellfun(@isempty,ix) % which are digits?
xc(idxdigit) % extract
Categories
Find more on Low-Level File I/O 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!