Skip empty line and alphabet
6 次查看(过去 30 天)
显示 更早的评论
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 .
0 个评论
回答(2 个)
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.
0 个评论
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!