Why does the IMPORTDATA function read in my file incompletely in MATLAB 7.6 (R2008a) and later?

11 views (last 30 days)
I am reading in a file, which contains rows with both text data and numeric data, using IMPORTDATA. The IMPORTDATA function stops reading in data from the file when it encounters text data after numeric data.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 1 Jul 2009
This is an expected behavior of the IMPORTDATA function.
The IMPORTDATA function is designed and intended to read a "rectangular" block of numeric data, possibly bordered by rows and/or columns of headers.
For your file, the IMPORTDATA function would detect the "data rectangle" as the block of numeric data that follows the initial lines of text data. These initial lines of text data would be read into the 'textdata' field of the output structure and the numeric data would be read into the 'data' field of the output structure. Any text data that follows the numeric data will be ignored.
For example, let 'Example_file.txt' contain both text and numeric data as follows:
Header
1
Day1 Day2 Day3 Day4 Day5 Day6 Day7
95.01 76.21 61.54 40.57 5.79 20.28 1.53
23.11 45.65 79.19 93.55 35.29 19.87 74.68
60.68 1.85 92.18 91.69 81.32 60.38 44.51
48.60 82.14 73.82 41.03 0.99 27.22 93.18
89.13 44.47 17.63 89.36 13.89 19.88 46.60
The command,
M = importdata('Example_file.txt');
will read in just the string 'Header' into the 'textdata' field of M and the number '1' into the 'data' field of M, ignoring the remaining data.
M =
data: 1
textdata: {'Header'}
rowheaders: {'Header'}
To read in the 5x7 block of data into the 'data' field, execute the following command instead:
M = importdata('Example_file.txt',' ',3);
The third argument '3' specified for the IMPORTDATA command indicates that the first 3 lines of the file will be treated as header text data. Hence, the 5x7 block of data will be read into the 'data' field of M.
M =
data: [5x7 double]
textdata: {3x7 cell}
colheaders: {'Day1' 'Day2' 'Day3' 'Day4' 'Day5' 'Day6' 'Day7'}

More Answers (0)

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Products


Release

R2008a

Community Treasure Hunt

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

Start Hunting!