How to only keep lines that start with a number?

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

Abu - is it just the first three lines or more? Can the lines with non-numerical data be anywhere in the list? Please clarify.
pudje
pudje on 14 Jul 2016
Edited: pudje on 14 Jul 2016
I have about 40 lines that start with Charactars, and then about 1000 lines that are just numbers. I would like to strip the text file of the first 40 lines so I can then apply dlm read to the text file.
Thanks!
Also, do you know of a good place to read about fopen and fid and fileid. I've been at it for a few hours and I'm just getting more confused. Like, is fileid just a common name to apply to the file object? Or is it syntax?

Sign in to comment.

Answers (2)

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')

1 Comment

What does the last line do?
I still have some reading to do about fid and fopen. Do you know of a good resource? I can't seem to get the hang of it.
Thanks!

Sign in to comment.

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

Tags

Asked:

on 14 Jul 2016

Answered:

on 14 Jul 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!