Skip empty line and alphabet

6 views (last 30 days)
Seong Wei Chin
Seong Wei Chin on 14 May 2016
Answered: Walter Roberson on 14 May 2016
1 1 -1 1 1 -1 -1 1 1 1 -1 1
1 -1 1 -1 -1 1 1 1 1 -1 1 1
sss
1 1 1 -1 1 -1 1 -1 -1 -1 -1 1
rtyyjjjj
1 -1 1 -1 -1 -1 1 1 -1 -1 -1 1
1 1 -1 1 1 -1 -1 -1 1 -1 1 -1
1 -1 1 1 1 1 1 -1 -1 -1 1 -1
I want to store the data into matlab as matrix, skipping the empty line and lines with alphabets.
How do i use textscan to perform this task or is there any better way? I used textscan and i get a= 8X1 cell.
Please help. Thank you .

Answers (2)

Image Analyst
Image Analyst on 14 May 2016
Use fgetl() and then skip any lines where the first character of the line you just read in is not a numeral.

Walter Roberson
Walter Roberson on 14 May 2016
textscan() of a file is not really suitable for that. Instead:
filecontent = fileread('YourFile.txt');
filecontent = regexprep(filecontent, {'^\s+', '^.*[^-+0123456789 \n].*', '\n\n'}, {'','','\n'}, 'lineanchors','dotexceptnewline');
YourData = cell2mat( textscan(filecontent, repmat('%f',1,12), 'CollectOutput', 1) );
This textscan()'s the string that has had the bad data thrown away.
This expression considers data to be bad if it contains anything other than spaces, plus sign, minus sign, or digits. In particular it does not take into account decimal points or floating point, and it does not know about the possibility of comments after the end of a line.

Community Treasure Hunt

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

Start Hunting!