Graphics, Figures & TablesCopy columns from multiple files into one table

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
dorienmc
Posts: 1
Joined: Wed Mar 04, 2015 1:20 pm

Copy columns from multiple files into one table

Post by dorienmc »

This question is a follow up of "Plotting averaged data from multiple files"(http://www.latex-community.org/forum/vi ... 45&t=24453). Here a method is shown how to import data from multiple files into one table.

Because I have multiple files with the same set of columnnames, which I want to show in one table, I tried to use to the \copycolumn macro (see thread in url) in combination with for each loops.

No error is produced but it does not add the specified columns. When I omit the foreach loops it does work. I assume the scope of the foreach loops messes up the variable that refers to the new table (\dataCombined in this case), i.e. \dataCombined is local in the foreach loops instead of global.

I have no idea how to fix this.

Code: Select all

\begin{filecontents}{testfile1.csv}
x,y,z
1,0.2,0.5
2,0.3,0.4
3,0.2,0.6
\end{filecontents}

\begin{filecontents}{testfile2.csv}
x,y,z
1,0.8,0.05
2,0.7,0.2
3,0.8,0.1
\end{filecontents}

\documentclass{article}
\usepackage{tikz, pgfplots, pgfplotstable,pgffor}	%extract table from csv
\pgfplotsset{compat=1.10}

%% Macro made by hugovdberg in thread described above
%% Copy column takes 4 arguments: input table, input column, output table, output column
\newcommand{\copycolumn}[4]{ 
\pgfplotstablecreatecol[   	                                                                                           
        col sep = comma, create col/copy column from table={#1}{#2}                                                      
        ]{#4}{#3}   
}


\begin{document}

\pgfplotstablenew[]{3}{\dataCombined}
\copycolumn{filetest1.csv}{x}{\dataCombined}{x}d
\foreach \file in {testfile1,testfile2}{
	\foreach \column in {y,z}{
		\copycolumn{\file.csv}{x}{\dataCombined}{\file-\column}
	}
}

\pgfplotstabletypeset [
   	col sep=comma,
	string type,
	]{\dataCombined}

\end{document}


Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

Post Reply