Skip empty line and alphabet

6 次查看(过去 30 天)
Seong Wei Chin
Seong Wei Chin 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