# Importing table from csv file

I have the following table in a avg_value.csv file:

,M23,F23_1,M36,F44,F63,M70
M23,1.00,0.81,0.88,0.83,0.41,0.82
F23_1,,1.00,0.52,0.56,0.25,0.94
M36,,,1.00,0.62,0.94,0.96
F44,,,,1.00,0.43,0.23
F63,,,,,1.00,0.22
M70,,,,,,1.00


and I want to import it in my LaTeX file. Currently, I am using this code from http://pgfplots.sourceforge.net/gallery.html :

\usepackage{pgfplots}
\usepackage{booktabs}
\usepackage{pgfplotstable}
\usepackage{array}
.
.
.
\pgfplotstabletypeset[
before row=\toprule,after row=\midrule},
every last row/.style={
after row=\bottomrule},
]
{pgfplotstable.avg_value.csv}


In fact, I do not import anything.

I receive as output message from kile:

! Package pgfplots Error: Could not read table file 'pgfplotstable.avg_value.csv'.

Changing the code as follows:

\documentclass{article}

\usepackage{pgfplots}

\pagestyle{empty}

\usepackage{pgfplotstable}
\usepackage{array}
\usepackage{colortbl}
\usepackage{booktabs}
\usepackage{amsmath}
\usepackage{pgfplotstable}

\begin{document}

\begin{table}
\pgfplotstabletypeset[
every last row/.style={after row=\bottomrule},
display columns/0/.style={string type,column name={}}
]
{\mytable}
\caption[title.]{title.}
\label{tab:foo}
\end{table}

\end{document}


did not solve the problem. Nothing really appears.

• Try renaming the file to "test.csv" Is it working then? If so, try to avoid the dot after "stable" Aug 23, 2015 at 7:21
• Changing the file into test.csv and the last line into {pgfplotstable.test.csv} does not solve the problem. Aug 23, 2015 at 7:29

You have to describe the structure of the csv file to pgfplotstable. And you have subscripts in your column/row names that you need to take care of. I've just removed them.

\documentclass{scrreprt}
\usepackage{pgfplotstable,booktabs}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% You don't need this part
% I did it to create your file
\usepackage{filecontents} % <-- To create files on the fly

\begin{filecontents*}{avg_value.csv}
,M23,F231,M36,F44,F63,M70
M23,1.00,0.81,0.88,0.83,0.41,0.82
F231,,1.00,0.52,0.56,0.25,0.94
M36,,,1.00,0.62,0.94,0.96
F44,,,,1.00,0.43,0.23
F63,,,,,1.00,0.22
M70,,,,,,1.00
\end{filecontents*}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\pgfplotstabletypeset[
col sep = comma,
string replace*={_}{\textsubscript},
every last row/.style={after row=\bottomrule},
display columns/0/.style={string type,column name={}}
]
{avg_value.csv}

\end{document}


Please have a look at the manual for a quickstart if you need to get a feeling for common options.

• This way does not import any .csv file. Aug 23, 2015 at 8:23
• @werty Read my second paragraph Aug 23, 2015 at 8:23
• Do you mean writing \pgfplotstableread[col sep=comma]{avg_value.csv}\mytable ? Aug 23, 2015 at 8:27
• @werty Yes exactly. Notice that underscore is a math active character in TeX in some cases it might cause problems so if you can, avoid it in file names and variables. Aug 23, 2015 at 8:29
• @DiaaAbidou \pgfplotstablegetelem can be used Dec 16, 2016 at 22:08