# Plotting functions with parameters from an external file using gnuplot/pgfplots/pgfplotstable

If I have a table, how can I get the table values and use them as parameters for plotting in pgfplots in gnuplot? In particular, how can multiple curves be plotted with the same functional form from multiple rows?

-

Edit: The first way I did this was with \def commands instead of \pgfmathsetmacro, which does not work.

Approach:

1. Load the table using \pgfplotstableread
2. Get the number of rows using \pgfplotstablegetrowsof, and decrement the number by one.
3. Within the axis, use \foreach to iterate over each row, up to the max value previously found
4. Use \pgfplotstablegetelem to retrieve the current row and the correct parameters, setting the variables (\a and \b) with \pgfmathsetmacro
5. Use the variables in the gnuplot command

Code:

\documentclass{standalone}

\usepackage{pgfplots,pgfplotstable,tikz}
\usepackage{filecontents}

\begin{filecontents}{data.dat}
a b
1 3
2 2
3 1
4 0
5 0.5
3 2
1 9
2 4
\end{filecontents}
\begin{document}

\pgfplotstablegetrowsof{\datafile}

\begin{tikzpicture}
\begin{axis}

{
\pgfplotstablegetelem{\rownum}{a}\of{\datafile}
\pgfmathsetmacro{\a}{\pgfplotsretval}
\pgfplotstablegetelem{\rownum}{b}\of{\datafile}
\pgfmathsetmacro{\b}{\pgfplotsretval}

set xrange [0:10];
set samples 100;
f(x) = \a*x+\b*x*x;
plot f(x);
};
}
\end{axis}
\end{tikzpicture}

\end{document}


-