How do I reformat a mixed format data file to work with readtable()?
7 views (last 30 days)
Show older comments
I have a database of several hundred CSV files which I want to read using readtable(). The first 2 rows of these files contain textual information (strings), the next 3 rows are headers/units followed by tens of thousands of rows of corresponding data. The files are CSV format, however, readtable() does not recognize the file structure because the first two rows do not contain any commas. It reads all the data in, but places it into a m*n x 1 array rather than an m x n array.
In my attempts to troubleshoot this I noticed that if I would first open the CSV file in excel then re-save the file (as CSV), the first two rows would get a series of commas which enable readtable() to recognize the table format. It will work fine after that, however, doing this for my whole database is not practical. That being said, can someone help me to either;
a) configure readtable() to ignore the first two lines
b) read the CSV (maybe as a text file?) in and add commas to rows 1 and 2? Note: not all of the files have the same number of columns so its not as easy as setting up simple loop to do this. There would need to be some kind of smarts to determine how many columns should be there based on the lines 3 through end. That is where I get a little fuzzy.
I have attached a truncated sample file for reference.
Note: I am using R2015a.
0 Comments
Accepted Answer
Peter Perkins
on 16 Jan 2018
In recent versions of MATLAB, you can use detectimportoptions to get more control over what gets read from where. But you may end up having to supplement readtable with a call to xlsread using a specific range to get some of your metadata.
More Answers (0)
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!