# Input tables as rows instead columns

Is there a way to read a table as rows instead of columns? Like the MWE example below illustrates:

\begin{filecontents*}{line123.dat}
x1  y1  y2      x3      y3
0   1   2       2       1
1   2   5       4.1     2
2   3   5       6.1     3
4   5   3       8.1     5
7   9   1       10.1    9
\end{filecontents*}
% I prefer my format to be as follows:
% \begin{filecontents*}{transposed_line123.dat}
%     x1 0 1 2 4 7
%     y1 1 2 3 5 9
%     y2 2 5 5 3 1
%     x3 2 4.1 6.1 8.1 10.1
%     y3 1 2 3 5 9
% \end{filecontents*}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\end{axis}
\end{tikzpicture}
\end{document}


The reason is because I have more columns that rows in line123.dat.

• Hi, Have you forgotten the\documentclass that you have used into the your preamble. Feb 27, 2018 at 14:28
• I don't believe I have. It's right after the filecontents* environments. Feb 27, 2018 at 14:30
• @Sebastiano Look carefully at the MWE, the \documentclass is present. Feb 27, 2018 at 15:16

You could use \pgfplotstabletranspose:

\begin{filecontents*}{transposed_line123.dat}
x1 0 1 2 4 7
y1 1 2 3 5 9
y2 2 5 5 3 1
x3 2 4.1 6.1 8.1 10.1
y3 1 2 3 5 9
\end{filecontents*}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.14}

\begin{document}
\pgfplotstabletranspose[input colnames to={x1},colnames from={x1}]{\mytabletoplot}{transposed_line123.dat}

\begin{tikzpicture}
\begin{axis}

• Thanks! I also found out that there is no need to load the "untransposed" table. \pgfplotstabletranspose can take the filename as the last input. Feb 27, 2018 at 16:41
• Mmm.. For some reason when the data is like this: x1 0 1 2 nan nan \n y1 1 2 3 5 9  Then pgfplots gives a warning: Table 'transposed_line123.dat' has non-unique column name 'nan'. Only the first occurence can be accessed via column names. I am guessing this is a bug. It's not major, but I like my latex documents warning free :) Any idea if this can be circumvented? Feb 28, 2018 at 17:27
• \n is just my way of saying there should be a new line there. If I compile the example that you include I get: Package pgfplots Warning: Table 'transposedwithnan.dat' has non-unique column n ame 'nan'. Only the first occurence can be accessed via column names. on input line 9. Mar 1, 2018 at 13:43
• @Tohiko Yes, but it's only a warning, the plot in not influenced, see this: \begin{filecontents*}{transposedwithnan.dat} x1 0 1 2 nan nan 4 y1 1 2 3 5 9 10 \end{filecontents*} \documentclass[border=5pt]{standalone} \usepackage{booktabs} \usepackage{pgfplotstable} \pgfplotsset{compat=1.14} \begin{document} \pgfplotstabletranspose[input colnames to={x1},colnames from={x1}]\mytabletoplot{transposedwithnan.dat} \begin{tikzpicture} \begin{axis} \addplot table [x=x1, y=y1] {\mytabletoplot}; \end{axis} \end{tikzpicture} \end{document} Mar 1, 2018 at 13:54