How to read a text file into a numeric array?

조회 수: 3 (최근 30일)
Mohamed Abokersh
Mohamed Abokersh 2018년 12월 2일
편집: Jeremy Hughes 2018년 12월 2일
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.

답변 (1개)

Jeremy Hughes
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};

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by