Manual for Package pgfplots
2D/3D Plots in LATeX, Version 1.18.1
http://sourceforge.net/projects/pgfplots
Import/Export From Other Formats
8.2Importing From Matlab
8.2.1Importing Mesh Data From Matlab To PGFPlots¶
While it is easy to write Matlab vectors to files (using save P.dat data -ASCII), it is more involved to export mesh data.
The main problem is to communicate the mesh structure to pgfplots.
Here is an example how to realize this task: in Matlab, we have mesh data X, Y and Z which are matrices of the same size. For example, suppose we have
[X,Y] = meshgrid( linspace(-1,1,5), linspace(4,5,10) );
Z = X + Y;
surf(X,Y,Z)
as data. Then, we can generate an \(N \times 3\) table containing all single elements in column by column ordering with
data = [ X(:) Y(:) Z(:) ]
% or -ascii
save P.dat data -ASCII
size(X)
ans =
10.00 5.00
where the second command stores the \(N \times 3\) table into P.dat. Finally, we can use
\addplot3[surf,mesh/rows=10,mesh/ordering=colwise,shader=interp] file {P.dat};
in pgfplots to read this data. We need to provide either the number of rows (\(10\) here) or the number of columns – and the ordering (which is colwise for Matlab matrices).
An alternative which is faster in pgfplots would be to transpose the matrices in Matlab and tell pgfplots they are in rowwise ordering. So, the last step becomes
XX=X'; YY=Y'; ZZ=Z';
data = [ XX(:) YY(:) ZZ(:) ]
save P.dat data -ASCII
with pgfplots command
\addplot3[surf,mesh/cols=10,mesh/ordering=rowwise,shader=interp] file {P.dat};.
8.2.2matlab2pgfplots.m¶
This is a Matlab® script which attempts to convert a Matlab figure to pgfplots. It requires Matlab version 7.4 (or higher).
This script is largely outdated and supports only a very small subset of pgfplots. You may want to look at matlab2tikz, a conversion script of Nico Schlömer available at
http://www.mathworks.com/matlabcentral/fileexchange/22022-matlab2tikz
which also uses pgfplots for the LaTeX conversion.
The idea of matlab2pgfplots.m is to
-
• use a complete Matlab figure as input,
-
• acquire axis labels, axis scaling (log or normal) and legend entries,
-
• acquire all plot coordinates
and write an equivalent .pgf file which typesets the plot with pgfplots.
The intention is not to simulate Matlab. It is a first step for a conversion. Type
> help matlab2pgfplots
on your Matlab prompt for more information about its features and its limitations.
This script is experimental.
8.2.3matlab2pgfplots.sh¶
A bash-script which simply starts Matlab and runs
f=hgload( 'somefigure.fig' ); matlab2pgfplots( 'outputfile.pgf', 'fig', f );
See matlab2pgfplots.m above.
8.2.4Importing Colormaps From Matlab¶
Occasionally, you may want to reuse your Matlab colormap in pgfplots. Here is a small Matlab script which converts it to pgfplots:
C = colormap; % gets data of the current colormap.
% C = colormap(jet) % gets data of "jet"
eachnth = 1;
I = 1:eachnth:size(C,1); % this is nonsense for eachnth=1 -- but perhaps you don't want each color.
CC = C(I,:);
TeXstring = [ ...
sprintf('\\pgfplotsset{\n\tcolormap={matlab}{\n') ...
sprintf('\t\trgb=(% f,%f,%f)\n',CC') ...
sprintf('\t}\n}\n') ]