File Exchange Pick of the Week

Reading Formatted Text 5

Posted by Jiro Doke,

Jiro's pick this week is TEXTSCANTOOL by our very own Stuart McGarrity.

Contents

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.

My Data File

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.

Header Lines

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:

Data Types

Next, you can individually specify the data types of the columns:

Import and Generate Code

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!

Video Tutorial

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.

Comments

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

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′)…

Thanks!

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!)

These postings are the author's and don't necessarily represent the opinions of MathWorks.