How to read a text file into a numeric array?
15 次查看(过去 30 天)
显示 更早的评论
This is probably trivial, but I have not found an easy way to convert a text file charaters inside (sample attached) into a Numeric Array with n lines and m columns. I tried several function including textscan, and sscanf. However non of them could read the text file in approparte way.
0 个评论
回答(1 个)
Jeremy Hughes
2018-12-2
编辑:Jeremy Hughes
2018-12-2
This was a tricky one. I was able to get something in, but there are extra rows at the end that aren't importable.
This file appears to have been generated as a human readable file, so it's not surprising a machine has some trouble.
opts = delimitedTextImportOptions('Delimiter',{'\t',' '},'NumVariables',17);
opts = setvartype(opts,2:16,'double');
opts.ConsecutiveDelimitersRule = 'join';
opts.DataLines = [3, inf]; % could also be [3 38] if you don't want the trailing rows
opts.VariableNamesLine = 2;
T = readtable('ANN_month.txt',opts,'ReadVariableNames',true);
head(T)
If you need a matrix:
A = T{:,2:16};
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!