4

I am using pgfplots to create figures, and I have a (large) set of simulation data generated from Matlab. It is in the .mat format. The data are of the form (two column)

(1,0.2)

(1,0.3)

...

(1,0.2)

...

(2,0.4)

(2,0.1)

...

A scatter plot in pgfplots would be appropriate. I want to directly importing this file using pgfplots. Unfortunately, the following code seems not work.

\documentclass{article}
\usepackage{pgfplots}
\usepackage{xcolor}

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}
\addplot+[scatter]
table{test.mat};
\end{axis}
\end{tikzpicture}
\end{figure}

\end{document} 

Is there anyone know how to import these data ? Since the size is a little bit large, it is not possible to copy every sample into the code.

  • What do you mean by "two columns"? The data in your file have the parenthesis or not? – Red Jul 15 '13 at 8:31
  • @Red No parenthesis. For instance, it is a 100 * 2 data set. The first column of the first 10 rows is 1, and the first column of the 11 to 20 rows is 2, etc. – Richie Jul 15 '13 at 8:35
  • What error do you get? I made a file test.mat with two rows, the first is "1 0.4" and the second is "1 0.5" and your code gives me no error – Red Jul 15 '13 at 8:38
  • @Red The compiler says "Text line contains an invalid character", and this error also appears in the simplest case, as the example you tried. – Richie Jul 15 '13 at 8:45
  • 3
    @Red matlab uses binary compression for .mat files and TeX can't read binary. You can use save('myfile.txt', 'myvariablename', '-ascii','-double'); in matlab to save it as a readable .txt file. – percusse Jul 15 '13 at 8:49
9

This is due to the fact that .mat file is a binary format and TeX can't read binary. I don't know how it can be made possible to read binary format from a file but a rather more convenient way is to store the arrays in a tall matrix as columns.

Suppose you have the data columns a,b,c with equal length (otherwise pad the missing entries with NaN or Inf such that pgfplots can discard it). This can be done in matlab via

Mymatrix = [a b c];
save('myfile.txt', 'Mymatrix', '-ascii','-double');

This would save the arrays in a plain text format with no headers. I didn't check if full path is supported in the file name field but should be possible.

In general, I would recommend staying away from including column names to the Mymatrix. Because matlab is a great software.

  • I mean including column names inside matlab. You can always edit the file and add them externally. – percusse Jul 15 '13 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.