Read CSV files as a Matlab struct array

I like CSV files for their portability (Python, Excel), ease of debug, and lightweight nature (no SQL databases etc). However, one key aspect of CSV files is that they are column-oriented–that is, you have to keep track of which data is in which column.
In python, csv.DictReader and csv.DictWriter do this for you–you don’t have to keep track of which data is in which column; the reader/writer does this for you.
I was looking for something similar in Matlab, so that I can read CSV files. In Matlab, the structure array is ideal for this, as it lets you create an array (one record per row) with structured data (one field per column).
The following file is something I wrote to read in CSV data. It returns a structure array, where the field names are given by a header line in the CSV file. It supports both numeric and string data in the CSV fields, with one caveat: all text-valued fields must occur contiguously and be the first columns in the file.

This entry was posted in Analog Professional and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

You must be logged in to post a comment.