[Tex/LaTex] Apply Math operations on loaded data inside PGF/Tikz plot

pgfmathpgfplotstabletikz-pgf

What I want: \abs{S21/S12-1}*100% where S21 and S12 are columns in txt-files

What I have: no idea; really.
I am profoundly unsure, if it is even possible in PGF/tikz to make this calculation.
If not, what alternatives would there be in TeX? PGFmath?

(I can still just take all the stuff to Matlab, calculate, re-import in TeX, but … meh.)

What I tried
I read the following three posts and tried to adapt: 1, 2 and 3 – where the latter gave me the idea, that basic operations should be possible but tricky to achieve because of the parser.

Below is a MWE (optimistically called; it does not compile) of what i want to get.

\documentclass{article}
\usepackage{amsmath,amsthm}
\usepackage{tikz} 
\usetikzlibrary{calc}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.14}

% sample values, extracted from 100/dB_IC_10mA_S21.txt and 100/dB_IC_10mA_S12.txt 

\pgfplotstableread[col sep=space]{% 
f1  s21
3.00000000000000000E5   -5.37923824569095640E-1
9.24624999999999990E5   -5.23021516857375080E-1
1.54925000000000020E6   -5.24032299826929030E-1 
2.17387499999999980E6   -5.18575016774617570E-1
2.79850000000000020E6   -5.17614614317642550E-1
3.42312500000000020E6   -5.28421012169730810E-1
4.04774999999999970E6   -5.25424891213584110E-1
4.67237500000000060E6   -5.24097415339730870E-1 
5.29700000000000060E6   -5.28626243766300340E-1
5.92162500000000060E6   -5.38603437681817350E-1
6.54625000000000060E6   -5.24872720444434120E-1
7.17087499999999970E6   -5.26809061504114420E-1
7.79549999999999970E6   -5.21513983574569910E-1
8.42012500000000050E6   -5.35052438394172110E-1
}\datas

\pgfplotstableread[col sep=space]{% 
f2  s12
3.00000000000000000E5   -4.61253023410550080E-1
9.24624999999999990E5   -4.44818670150491350E-1
1.54925000000000020E6   -4.45906534066983800E-1
2.17387499999999980E6   -4.54506559283727720E-1
2.79850000000000020E6   -4.50208906640044760E-1
3.42312500000000020E6   -4.52753227856168470E-1
4.04774999999999970E6   -4.53657966529585540E-1
4.67237500000000060E6   -4.64282209437626040E-1
5.29700000000000060E6   -4.51387968956751350E-1
5.92162500000000060E6   -4.59361196203085690E-1
6.54625000000000060E6   -4.66558553131420340E-1
7.17087499999999970E6   -4.44614963756639090E-1
7.79549999999999970E6   -4.57214959955166740E-1
8.42012500000000050E6   -4.54436434169834770E-1
}\datat
\begin{document}    
\begin{tikzpicture}
    \begin{axis}[
    no markers,%
    ]
%       \addplot+[thin] table [col sep=space]{100/dB_IC_10mA_S12.txt};  
% complete file of measurement data
%       \addplot+[thin] table [col sep=space]{100/dB_IC_10mA_S21.txt};  
% complete file of measurement data
%       \addplot+[thin] table [col sep=space]\abs{{100/dB_IC_10mA_S12.txt}-{100/dB_IC_10mA_S21.txt}}; 
% not working
%       \addplot+[thin] table [col sep=space]\abs{\datas - \datat}; 
% not working
    \addplot+[thin] table [col sep=space] (${\abs{\datas - \datat}}$);
    \addplot+[thin] table[col sep=space, x=(${\abs{\datas - \datat}}$),y=f2] 
    \end{axis}
    \end{tikzpicture} 
\end{document}

Best Answer

First I think you need to merge the two tables, to be able to use data from both in the same plot. See e.g. merge two tables into one for a way. Basically, you can insert a column from one table into another using

\pgfplotstablecreatecol[
  copy column from table={\datas}{[index] 1},
  ]{s21}{\datat}

To plot the result of a calculation based on columns in a table, you can use y expr=<calculation>, and in the calculation you can use \thisrow{<column name>} to access values in the table. So if I understand correctly, you want y expr={abs(\thisrow{s21}/\thisrow{s12}-1)*100}, which is used as

\addplot+[thin] table[x=f1,y expr={abs(\thisrow{s21}/\thisrow{s12}-1)*100}] {\datat};  

Complete code:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.13}

% sample values, extracted from 100/dB_IC_10mA_S21.txt and 100/dB_IC_10mA_S12.txt 

\pgfplotstableread[col sep=space]{% 
f1  s21
3.00000000000000000E5   -5.37923824569095640E-1
9.24624999999999990E5   -5.23021516857375080E-1
1.54925000000000020E6   -5.24032299826929030E-1 
2.17387499999999980E6   -5.18575016774617570E-1
2.79850000000000020E6   -5.17614614317642550E-1
3.42312500000000020E6   -5.28421012169730810E-1
4.04774999999999970E6   -5.25424891213584110E-1
4.67237500000000060E6   -5.24097415339730870E-1 
5.29700000000000060E6   -5.28626243766300340E-1
5.92162500000000060E6   -5.38603437681817350E-1
6.54625000000000060E6   -5.24872720444434120E-1
7.17087499999999970E6   -5.26809061504114420E-1
7.79549999999999970E6   -5.21513983574569910E-1
8.42012500000000050E6   -5.35052438394172110E-1
}\datas

\pgfplotstableread[col sep=space]{% 
f2  s12
3.00000000000000000E5   -4.61253023410550080E-1
9.24624999999999990E5   -4.44818670150491350E-1
1.54925000000000020E6   -4.45906534066983800E-1
2.17387499999999980E6   -4.54506559283727720E-1
2.79850000000000020E6   -4.50208906640044760E-1
3.42312500000000020E6   -4.52753227856168470E-1
4.04774999999999970E6   -4.53657966529585540E-1
4.67237500000000060E6   -4.64282209437626040E-1
5.29700000000000060E6   -4.51387968956751350E-1
5.92162500000000060E6   -4.59361196203085690E-1
6.54625000000000060E6   -4.66558553131420340E-1
7.17087499999999970E6   -4.44614963756639090E-1
7.79549999999999970E6   -4.57214959955166740E-1
8.42012500000000050E6   -4.54436434169834770E-1
}\datat

\pgfplotstablecreatecol[
  copy column from table={\datas}{[index] 0},
  ]{f1}{\datat}
\pgfplotstablecreatecol[
  copy column from table={\datas}{[index] 1},
  ]{s21}{\datat}
\begin{document}    
\begin{tikzpicture}
    \begin{axis}[
    no markers,%
    ]
       \addplot+[thin] table[x=f1,y=s21] {\datat};  
       \addplot+[thin] table[x=f2,y=s12] {\datat};  
    \end{axis}
    \end{tikzpicture} 

\begin{tikzpicture}
    \begin{axis}[
    no markers,%
    ]
       \addplot+[thin] table[x=f1,y expr={abs(\thisrow{s21}/\thisrow{s12}-1)*100}] {\datat};  
    \end{axis}
    \end{tikzpicture} 
\end{document}
Related Question