[pstricks] Accessing data in files on disk

Denis Girou Denis.Girou at idris.fr
Thu Apr 24 23:52:00 CEST 2003


>>>>> "Alan.Ristow" == Alan Ristow <ristow at ece.gatech.edu> writes:

    Alan.Ristow> I am trying to write some highly customized plotting routines. Ideally,
    Alan.Ristow> my data would be stored in a text file on disk, and could come in one,
    Alan.Ristow> two, or more columns. Is there a simple way to access this data, one
    Alan.Ristow> point at a time, after it has been read by \readdata? Clearly PSTricks
    Alan.Ristow> does this somehow in pst-plot, but after poring over the code for awhile
    Alan.Ristow> I can't figure out how.

  In fact, the better way to proceed is not easy to guess, as it depend of
what kind of actions you plan to do on these data (which will be coordinates
only, as I understand it). Mainly, it depend of who will manage them, TeX or
PostScript. What \readdata do is to concatenate in a macro the coordinates,
suppressing the original delimiters and introducing a "D" Postscript operator
before each of them. For instance, with the "data.dat" file that I use for my
tests here, the macro resulting of \readdata will contain:
 D 1.2 D 2.3 D 2.7 D -1.1 D 2 D 2 D 4.5 D 0.7539 D 1.7 D 3.9 D 1 D 4.6 D 
-0.43 D 3 D 3.98 D -0.7 D -0.7 D -0.4

'data.dat' file:
----------------------------------------------------------------------
(1.2,2.3)
(2.7,-1.1) (2,2)       (4.5,0.7539)
(1.7,3.9)  (1,4.6)
(-0.43,3)  (3.98,-0.7) (-0.7,-0.4)
----------------------------------------------------------------------

  The more efficient and powerful solution for you could be to just use this
feature and play with the "D" operator. This is how the \dataplot macro work.

  But if this way is not relevant for what you want to do and if you want to
manage the coordinates by yourself at the TeX level, you have only to parse
the macro resulting from \readdata (what you seemed to want.) I give you an
example where you have just to define the \DoCoordinate macro to specify what
to do with each pair of coordinates (in my examples, I just print them or put
a dot at the position of the coordinate.)

\documentclass[a4paper]{article}

\usepackage{pst-plot}

\makeatletter

\def\GetCoordinates#1{\expandafter\GetCoordinates at i#1}

\def\GetCoordinates at i #1{\GetCoordinates at ii#1}

\def\GetCoordinates at ii#1 #2 #3 #4 {%
\DoCoordinate{#2}{#4}%
\@ifnextchar D{\GetCoordinates at ii}{}}

\makeatother

\pagestyle{empty}

\begin{document}

\readdata{\Data}{data.dat}

\def\DoCoordinate#1#2{\typeout{New point: (#1,#2)}}
\GetCoordinates{\Data}

\begin{pspicture}(-1,-2)(5,5)
  \psgrid[subgriddiv=0]
  \psset{dotscale=2}%
  \def\DoCoordinate#1#2{\psdot(#1,#2)}%
  \GetCoordinates{\Data}
\end{pspicture}

\end{document}


D.G.


More information about the PSTricks mailing list