Import text files with character and numeric data

7 views (last 30 days)
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.

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 16 Jul 2015
s=importdata('file.txt')
data=s.data
text=s.textdata
colheaders=s.colheaders
  9 Comments
Cedric
Cedric on 17 Jul 2015
Edited: Cedric on 17 Jul 2015
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 on 17 Jul 2015
Edited: Cedric on 17 Jul 2015
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

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!