Skip empty line and alphabet

조회 수: 6 (최근 30일)
Seong Wei Chin
Seong Wei Chin 2016년 5월 14일
답변: Walter Roberson 2016년 5월 14일
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 .

답변 (2개)

Image Analyst
Image Analyst 2016년 5월 14일
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 2016년 5월 14일
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.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by