Read specific column from .txt file with unkown format

조회 수: 1 (최근 30일)
Ilhem
Ilhem 2017년 10월 12일
댓글: Cedric 2017년 10월 13일
Hello all, I am a beginner in matlab. I’m trying to import specific rows from a .txt file where the file format is unknown. Actually, the existing solutions allow me only to get rows instead of columns. For example, I want to read from the attached file, the column n=2 that includes (V1 D3 6.52 4.91 3.00 2.05 0.69 NAN NAN NAN) and to store the result into an array.
  댓글 수: 1
Cedric
Cedric 2017년 10월 12일
There must be a few things that you know about the format. Do you need the first rows or just the numeric data? Is the first row always containing those V + a column index or not?

댓글을 달려면 로그인하십시오.

채택된 답변

Cedric
Cedric 2017년 10월 12일
편집: Cedric 2017년 10월 12일
content = fileread( 'Info.txt' ) ;
nCols = numel( strsplit( regexp( content, '[^\r\n]+', 'match', 'once' ), ' ')) ;
data = reshape( regexp(content, '\s+', 'split'), nCols, [] ).' ;
header = data(1:2,:) ;
data = str2double( data(3:end,:) ) ;
and then you can pick any column in both header and data. Or you can use the simpler:
data = importdata( 'Info.txt' ) ;
and see which field provides you with what you need.
  댓글 수: 5
Ilhem
Ilhem 2017년 10월 13일
That works well. Thank you!
Cedric
Cedric 2017년 10월 13일
My pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by