In many of my projects, reading in the data files is often the first step. I utilize various methods, ranging from double-clicking on the data file to using high-level import functions (such as xlsread and load) to using low-level functions (such as textscan and fread). The more unconventional the data format is, the more I rely on low-level functions.
Let's take a look at this particular data file:
Have you ever had to deal with this type of format - comma-separated file, arbitrary number of header lines, a row with label names, and a mix of numeric and text data? I have, quite often.
Stuart's textscantool allows you to easily bring this data in, by working in conjunction with MATLAB's textscan function. It provides a nice graphical interface to quickly parse through a formatted ascii file and construct an automated import function for reading similar files.
The tool takes you through a sequence of steps to import a file. First, you can indicate how many header lines there are and which row will be used for the header names:
Next, you can individually specify the data types of the columns:
Finally, you can specify how to bring it in (array, cell, etc) and how many rows to import. This means that you can import a single portion of a large file.
And you click "Import Data" and off you go! Want to automate this process? Just click on "Generate Code", and you have a reusable function!
What makes this entry complete is the video tutorial that Stuart includes with his function. And yes, he's the voice of many of our shipping tutorial videos.
MATLAB provides numerous functions for importing files. Tell us here how you use these functions to deal with your specific data files.
Get the MATLAB code
Published with MATLAB® 7.6
5 CommentsOldest to Newest
sweet. this serves a great need.
looks like a great utility.
Great! But for it to be awesome it should handle numerical values with commas in it.
Cyrock, could you share a sample (just a few lines) to make the problem case clear? Thanks!
Great! This is very convinient to use. But I use matlab codes for opening the data and reading/writing. Such as fid = fopen(…);fread(fid,inf,’int64′)…
Holy mackerel! I’m new to matlab, but the import process was killing me. This thing is amazing. I can’t believe that Mathworks doesn’t have a tool that’s exactly like this.
My only suggestions:
+ Read the first few row(s) of data to test columns for number values for smarter defaults (rather than all strings). I wrote something like this in VBA if you’d like it.
+ Include brief data definitions for the data types (do I need an int32 or int64? I don’t know!)