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

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Post Reply