How can I read in a fixed width ASCII file which contains missing numeric data in MATLAB?
7 views (last 30 days)
Show older comments
MathWorks Support Team
on 27 Jun 2009
Edited: MathWorks Support Team
on 17 May 2023
I have a fixed-width (or fixed-length) ASCII file which contains numeric and text fields. Some of the data in the numeric fields is missing. I am unable to read in this data using the TEXTSCAN function in conjunction with the string and numeric formatting options.
Here is an example of some fixed width data I am trying to read:
Jon123.4Mr
Ann27832Ms
Max Dr
The last line of data has five spaces between the 'Max' and the 'Dr'. This data is located in a file called test.dat. I attempted to read it in the following way:
fid = fopen('test.dat');
fmt = '%3s %5.2f %2s';
D = textscan(fid,fmt,'whitespace','')
fclose(fid);
When I execute this, the missing field does not appear as an empty array and the last field, 'Dr', is not read in.
Accepted Answer
MathWorks Support Team
on 17 May 2023
Edited: MathWorks Support Team
on 17 May 2023
As of R2017a, you can use a "FixedWidthImportOptions" object to specify how MATLAB imports fixed-width tabular data from text files. Please see the following documentation page for more information:
Prior to R2017a, the ability to read in fixed width data with missing numeric values was not available in MATLAB.
To work around this issue, read in the fields as strings of the appropriate widths. Then convert the appropriate fields to numbers using the STR2NUM functions. This can be done is a single line with the CELLFUN function. Setting UniformOutput to false allows for missing values. For example:
fid = fopen('test.dat');
fmt = '%3s %5s %2s';
D = textscan(fid,fmt,'whitespace','');
D{2} = cellfun(@str2num, D{2}, 'UniformOutput', false)
fclose(fid);
At the end of this script, the variable "D" holds the text and numeric data in a cell array.
4 Comments
per isakson
on 4 Nov 2017
Edited: MathWorks Support Team
on 18 Apr 2019
At last, see FixedWidthImportOptions, Import options object for fixed-width text files. Introduced in R2017a
More Answers (0)
See Also
Categories
Find more on Text Files in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!