# edit x-y data from different files/tables

I've two one column data files (A.txt and B.txt). To plot them together first I load dataA with

\pgfplotstableset{
create on use/dataA/.style={create col/copy column from table={A.txt}{0}}
}


Then I put them together with

\begin{figure}[ht!]
\begin{tikzpicture}
\begin{axis}
\addplot table [x =dataA,y index=0,y expr=\thisrowno{0}*-1] {B.txt};
\end{axis}
\end{tikzpicture}
\end{figure}


I've no problem to change the sign of the data from B.txt as so can see above.... BUT:

How to do if I want to change the sign from the loaded table dataA??

Has anybody got a helpful idea or minimal example?

EDIT: A minimal example could look like this:

\documentclass{article}
\usepackage{pgfplots, pgfplotstable}
\usepackage{filecontents}
\begin{filecontents}{A.txt}
2
3
4
5
3
5
\end{filecontents}
\begin{filecontents}{B.txt}
5
4
3
2
5
5
\end{filecontents}

\pgfplotstableset{
create on use/dataA/.style={create col/copy column from table={A.txt}{0}}
}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot table [x=dataA , y index=0 , y expr=\thisrowno{0}*-1] {B.txt};
\end{axis}
\end{tikzpicture}
\end{document}


-

Usually, you'd be able to access the column by name using x expr=\thisrow{dataA}*-1. However, this only works if the column is already present in the table, not if it's generated on the fly, as is the case here.

One way to get around this is to read the first table into a table macro using \pgfplotstableread{B.txt}{\datatable} and then explicitly create the new column using \pgfplotstablecreatecol[copy column from table={A.txt}{0}] {dataA} {\datatable}. Then \addplot table [x expr=\thisrow{dataA}*-1 , y index=0 , y expr=\thisrowno{0}*1] {\datatable}; works as expected.

Alternatively, you could use an x filter/.code to modify the x values. Within the x filter/.code, the current coordinate is available as \pgfmathresult, and the same macro is expected to contain the altered coordinate at the end of the filter. So you could use x filter/.code=\pgfmathparse{-1*\pgfmathresult} to swap the sign here.

Code using explicitly created column:

\documentclass{article}
\usepackage{pgfplots, pgfplotstable}
\usepackage{filecontents}
\begin{filecontents}{A.txt}
2
3
4
5
3
5
\end{filecontents}
\begin{filecontents}{B.txt}
5
4
3
2
5
5
\end{filecontents}

\pgfplotstablecreatecol[copy column from table={A.txt}{0}] {dataA} {\datatable}

\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot table [x=dataA , y index=0 , y expr=\thisrowno{0}*1] {\datatable};
\addplot table [x expr=\thisrow{dataA}*-1 , y index=0 , y expr=\thisrowno{0}*1] {\datatable};
\end{axis}
\end{tikzpicture}
\end{document}


Code using x filter/.code:

\documentclass{article}
\usepackage{pgfplots, pgfplotstable}
\usepackage{filecontents}
\begin{filecontents}{A.txt}
2
3
4
5
3
5
\end{filecontents}
\begin{filecontents}{B.txt}
5
4
3
2
5
5
\end{filecontents}
\pgfplotstableset{ create on use/dataA/.style={create col/copy column from table={A.txt}{0}} }

\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot table [x=dataA , y index=0 , y expr=\thisrowno{0}*1] {B.txt};
table [x=dataA , y index=0 , y expr=\thisrowno{0}*1] {B.txt};
\end{axis}
\end{tikzpicture}
\end{document}

-
you are a genius! thanks! –  David K. Aug 14 '12 at 12:42
@DavidK.: Thanks =). I've edited my answer to include a different approach that might be more comfortable to use. –  Jake Aug 14 '12 at 12:44
Do you have an idea how can I sum up all lines of an onecolumn file A.txt B.txt C.txt in order to get my y-data for my axis environment (in detail: y(1)=A(1)+B(1)+C(1)... etc.). The x-data are saved in a seperate file X.txt . –  David K. Jan 14 '13 at 17:25
@DavidK.: Yes, but it requires a bit of code. It's probably best to open a new question. If you do, it would be good if you could describe how flexible you need the solution to be (will you always have exactly three files, are the names of the files systematic, etc.) –  Jake Jan 14 '13 at 17:41
thanks, ok then I'll open up a new and try my best to describe it more in detail. –  David K. Jan 14 '13 at 17:43