File Exchange Pick of the Week

June 6th, 2008

Reading Formatted Text

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 Responses to “Reading Formatted Text”

  1. Marc replied on :

    sweet. this serves a great need.

    looks like a great utility.

  2. Cyrock replied on :

    Great! But for it to be awesome it should handle numerical values with commas in it.

  3. Bob replied on :

    Cyrock, could you share a sample (just a few lines) to make the problem case clear? Thanks!

  4. Berdakh Abibullaev replied on :

    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!

  5. Gene replied on :

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

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Bob, Brett & Jiro share their favorite user-contributed submissions from the File Exchange.

  • Zach: Hi Doug and Les, I didn’t have a lot of time to mess with this, but I did find a work-around. I plotted...
  • hamed: k
  • Les: @Zach This isn’t exactly what you are looking for but at least it puts all three parameters on the same...
  • Zach: Thanks for your suggestions Doug. I’ll give that a shot and see what happens. I’ve seen many of...
  • Doug: @Zach, I would say to use plotYYY, because that is close to what you want, but using depth as Y makes sense....
  • Doug: @Teja, I think this will work: http://www.mathworks .com/access/helpdesk /help/techdoc/ref...
  • Gify: merry christmas :) nice christmas tree! Regards, Janet Gify
  • Teja: Dear Doug Is there anyway to plot a surface from nonuniform data without meshgrid and griddata? Basically i...
  • Zach: I’m working with geophysical data, so I’d like to produce a depth profile. The y-axis would be...
  • Doug: @Ashok First, please do not use variable names that are MATLAB commands (std and mean). Second, p(j) should be...

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