Save Matlab data to PGFPlots
Suppose you have data (x,y)
or (x,y,z)
in Matlab. Now, you want to plot it in LaTeX using PGFPlots. These functions will help you.
- Download ZIP.
- Extract it.
- Copy files
saveplot2d.m
andsaveplot3d.m
to your code path. - Call functions
saveplot2d
orsaveplot3d
after your plot code.
-
saveplot2d ( filename, x, y)
Save all data fromy(x)
tofilename.txt
, and LaTeX code tofilename.tex
. -
saveplot2d ( filename, x, y, sx )
Save selected data fromy(x)
tofilename.txt
, and LaTeX code tofilename.tex
. Selectx
usingsx
.
x = linspace(0, 1, 101);
y = sin(10*x);
% Plot in Matlab
plot(x, y);
% Save PGFPlot figure with data
saveplot2d('fig1', x, y);
saveplot2d('fig2', x, y, 1:5:101);
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel={$x$},
ylabel={$y$},
]
\addplot[] table {fig1.txt};
\end{axis}
\end{tikzpicture}
\end{document}
-
saveplot3d ( filename, x, y, z )
Save all data fromz(x,y)
tofilename.txt
, and LaTeX code tofilename.tex
. -
saveplot3d ( filename, x, y, z, sx, sy )
Save selected data fromz(x,y)
tofilename.txt
, and LaTeX code tofilename.tex
. Selectx
andy
usingsx
andsy
respectively.
x = linspace(0, 1, 101);
y = linspace(-1, 1, 201);
z = exp(-x'.^2 - y.^2); % get z(x,y) as 101 x 201 matrix
% Plot 3D surface in Matlab
surf(x, y, z');
% Save PGFPlot figure with data
saveplot3d('fig3', x, y, z);
saveplot3d('fig4', x, y, z, 1:5:101, 1:5:201);
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel={$x$},
ylabel={$y$},
zlabel={$z$},
view={60}{30},
]
\addplot3[surf, mesh/rows=101] table {fig3.txt};
\end{axis}
\end{tikzpicture}
\end{document}
This project is licensed under the MIT License.