[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Specific fixed width question...

From: John W. Eaton
Subject: Specific fixed width question...
Date: Tue, 14 Sep 2010 15:18:16 -0400

On 14-Sep-2010, fork wrote:

| Two lines of example data (notice that the county field (#4) is 25 characters
| wide and that spaces are used both between fields like WA and 053 and between
| parts of the county name, so you can't just split on spaces):
| WA 053 011 Clark County             45
| WA 053 027 Grays Harbor County      100
| I would like to parse out cntys=[11; 27] and data=[45; 100] from the above.
| I realize that I can pre-process the file outside of Octave, but I don't want 
| for various reasons (lack of access to real Unix tools, mostly).

Something like this will work:

  fid = fopen ("foo.dat", "r")
  tmp = fscanf (fid, "%*2c %*3d %3d %*25c %3d")
  ctys = tmp(1:2:end)
  data = tmp(2:2:end)

If you need to extract mixed character and numeric data, things become
more complicated, but it is still possible.  You just may need to do
conversions on the data afterward, or you will need to break up the
reading into multiple calls to *scanf.  For example:

  tmp = sscanf ("WA 053 011 Clark County             45",
                "%2c %3d %3d %25c %3d")

  state = char (tmp (1:2)')
  f2 = tmp(3)
  cnty = tmp(4)
  name = char (tmp(5:29)')
  data = tmp(30)


reply via email to

[Prev in Thread] Current Thread [Next in Thread]