How do I read from a data file with varying number of columns with MATLAB 7.0 (R14)?
1 view (last 30 days)
Show older comments
I have a text file that contains data with varying number of columns. For example, one row might contain two strings followed by three numeric values, whereas the next row could contain two strings followed by two numeric values.
Accepted Answer
MathWorks Support Team
on 2 Feb 2011
As of MATLAB 7.0 (R14), you can use TEXTSCAN to read files of irregular format.
For example, consider a comma delimited data in line 4 of text file 'mydata.txt' that contains one line for a header followed by multiiple rows of data. The first two entries on the fifth row are strings, but there are four numeric entries after the strings.
In order to read the line 4 numeric values from text file into MATLAB, follow the steps below:
1. Download the attached files to your current working directory.
2. Run the MATLAB file 'sample.m'.
Note that TEXTSCAN will fill entries for which no element was found with NaN.
An alternative approach to TEXTSCAN is to use FGETL to read a file line-by-line.
0 Comments
More Answers (0)
See Also
Categories
Find more on Data Import and Export 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!