필터 지우기
필터 지우기

Import text files with character and numeric data

조회 수: 12 (최근 30일)
Karthik
Karthik 2015년 7월 16일
편집: Cedric 2015년 7월 17일
Hello, I have the following text file (please find attached). I want to import it into matlab and I need only numeric data. The text is not required. I tried this using the import function in matlab. The problem I have is the number of columns are not known and keeps on changing. So the generated code is not working when the number of columns change. How can I import the data with any number of columns and rows. Moreover, the data file I attached is a smaller version. The number of rows in original data file goes over 3 million. How can I import the text file of this type as fast as possible ?
Thank you.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2015년 7월 16일
s=importdata('file.txt')
data=s.data
text=s.textdata
colheaders=s.colheaders
  댓글 수: 9
Cedric
Cedric 2015년 7월 17일
편집: Cedric 2015년 7월 17일
The file number? You can build a string using SPRINTF, for example
for fileId = 1 : 10
filename = sprintf( 'Raw%d.txt', fileId )
content = fileread( filename ) ;
...
end
But you can also use DIR to get e.g. all text files, whatever their name:
D = dir( '*.txt' ) ;
for fileId = 1 : length( D )
filename = D(fileId).name ;
content = fileread( filename ) ;
...
end
This would catch Raw.txt for example, which has no number.
Cedric
Cedric 2015년 7월 17일
편집: Cedric 2015년 7월 17일
I just re-read your comment and realized that I misunderstood. The variable parameters is a struct, a variable with fields:
>> class( parameters )
ans =
struct
Its fields can be dot-indexed. If you want to address/index the field elay for example, you do it this way:
>> parameters.elay
ans =
11250
This is a numeric field of type/class double:
>> class( parameters.elay )
ans =
double
so you can compute with it:
>> parameters.elay / 10
ans =
1125

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by