How to remove lines that starts either : and # using regexp

I am interested only to screen rows that starts with number or skip those rows that starts with : and # Any idea ?

 Accepted Answer

filecontent = fileread('NameOfTheFile.txt');
newcontent = regexprep(filecontent, '^[#:].$', '', 'lineanchors', 'dotexceptnewline');
You can use textscan() on the string newcontent:
numcols = 18
fmt = repmat('%g', 1, numcols);
datacell = textscan(newcontent, fmt, 'CollectOutput', 1);
data = datacell{1};

5 Comments

@Walter Roberson Thank you for your support I am getting the following error. please see the sample file attached.
Error using textscan
Unable to parse the format string at position 1 ==> %g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g%g
Unsupported format specifier '%g'. See the documentation for TEXTSCAN for supported formats.
Thanks @Walter Roberson , the newcontent still has rows that stars with " :endHeader" and " :Fame 1 1"2002/01/01/ 07:00:00.000"". It only get rid of the rows that starts with # any idea?
filecontent = fileread('NameOfTheFile.txt');
newcontent = regexprep(filecontent, '^[#:].*$', '', 'lineanchors', 'dotexceptnewline');
numcols = 18;
fmt = repmat('%f', 1, numcols);
datacell = textscan(newcontent, fmt, 'CollectOutput', 1);
data = datacell{1};

Sign in to comment.

More Answers (0)

Tags

Asked:

on 12 Oct 2016

Commented:

on 13 Oct 2016

Community Treasure Hunt

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

Start Hunting!