# Sum up rows from different data files

I've a certain amount of inputfiles (.txt) for the final definition of the y-data of my axis environment. The x-data come from a different file. The goal is to sum up each row of the input files (necessary for my y-data) and multiply them by e.g -1.

So the files needed for my Y-data could look like the following:

\begin{filecontents}{n1b1_f1.txt}
2
3
\end{filecontents}

\begin{filecontents}{n1b2_f1.txt}
4
5
\end{filecontents}

\begin{filecontents}{n1b3_f1.txt}
6
7
\end{filecontents}

\begin{filecontents}{n1b4_f1.txt}
8
9
\end{filecontents}


And the file for the X-data like

\begin{filecontents}{X.txt}
1
2
\end{filecontents}


Now the final goal is to plot a x-y diagram with x(i)=X(i) and y(i)=(n1b1_f1(i)+n1b2_f1(i)+n1b3_f1(i)+n1b4_f1(i))*-1 where i gives the number of the current line.

The number of inputfiles needed for my y data could change, but not be greater than 9 and its now problem if I have to do this kind of adoptions 'manually'

I general I do not know the number of lines before. I would have to look them up, but it would be nice if it would work for any number i of lines automatically

-
If you just adding the data why don't you concatenate the files into one before processing? – Yiannis Lazarides Jan 14 '13 at 18:27
cause this would 'cost' me more time i think and im sure thats the better way – David K. Jan 14 '13 at 18:35

Here's a new macro \mergetables{<list of table names>}{<output table>} that you can use to join your tables into one. In your example, you could use

\mergetables{X, n1b1_f1, n1b2_f1, n1b3_f1, n1b4_f1}{\datatable}


where it is assumed that the files are called X.txt, n1b1_f1.txt, etc. In your plot, you would then use something like

\addplot table [y expr=(\tr{n1b1_f1}+\tr{n1b2_f1}-\tr{n1b3_f1})*-1]{\datatable};


to add three of the columns and multiply them by -1.

\documentclass{article}
\usepackage{pgfplots, pgfplotstable}
\usepackage{filecontents}

\begin{filecontents}{X.txt}
1
2
3
\end{filecontents}

\begin{filecontents}{n1b1_f1.txt}
2
3
5
\end{filecontents}

\begin{filecontents}{n1b2_f1.txt}
4
5
8
\end{filecontents}

\begin{filecontents}{n1b3_f1.txt}
6
7
2
\end{filecontents}

\begin{filecontents}{n1b4_f1.txt}
8
9
1
\end{filecontents}

\newcommand{\mergetables}[2]{
\newif\iffirstrow
\firstrowtrue

\pgfplotsforeachungrouped \tablename in {#1}{
\iffirstrow
\pgfplotstabletranspose[input colnames to=colnames]{\temptabletransposed}{\temptable}
\pgfplotstablemodifyeachcolumnelement{colnames}\of\temptabletransposed\as\cell{%
\edef\cell{\tablename}%
}
\firstrowfalse
\else
\pgfplotstabletranspose[input colnames to=colnames]{\temptabletransposedcur}{\temptable}
\pgfplotstablemodifyeachcolumnelement{colnames}\of\temptabletransposedcur\as\cell{%
\edef\cell{\tablename}%
}
\pgfplotstablevertcat{\temptabletransposed}{\temptabletransposedcur}
\fi
}
\pgfplotstabletranspose[colnames from=colnames, input colnames to=]{#2}{\temptabletransposed}
}
\def\tr{\thisrow}

\mergetables{X, n1b1_f1, n1b2_f1, n1b3_f1, n1b4_f1}{\datatable}

\begin{document}

\begin{tikzpicture}
\begin{axis}[ xlabel=$x$, ylabel=$y$]