Import/Export From Other Formats
\(\newcommand{\footnotename}{footnote}\) \(\def \LWRfootnote {1}\)
\(\newcommand {\footnote }[2][\LWRfootnote ]{{}^{\mathrm {#1}}}\)
\(\newcommand {\footnotemark }[1][\LWRfootnote ]{{}^{\mathrm
{#1}}}\) \(\let \LWRorighspace \hspace \) \(\renewcommand {\hspace
}{\ifstar \LWRorighspace \LWRorighspace }\) \(\newcommand
{\mathnormal }[1]{{#1}}\) \(\newcommand \ensuremath [1]{#1}\)
\(\newcommand {\LWRframebox }[2][]{\fbox {#2}} \newcommand
{\framebox }[1][]{\LWRframebox } \) \(\newcommand {\setlength
}[2]{}\) \(\newcommand {\addtolength }[2]{}\) \(\newcommand
{\setcounter }[2]{}\) \(\newcommand {\addtocounter }[2]{}\)
\(\newcommand {\arabic }[1]{}\) \(\newcommand {\number }[1]{}\)
\(\newcommand {\noalign }[1]{\text {#1}\notag \\}\) \(\newcommand
{\cline }[1]{}\) \(\newcommand {\directlua }[1]{\text
{(directlua)}}\) \(\newcommand {\luatexdirectlua }[1]{\text
{(directlua)}}\) \(\newcommand {\protect }{}\) \(\def
\LWRabsorbnumber #1 {}\) \(\def \LWRabsorbquotenumber "#1 {}\)
\(\newcommand {\LWRabsorboption }[1][]{}\) \(\newcommand
{\LWRabsorbtwooptions }[1][]{\LWRabsorboption }\) \(\def \mathchar
{\ifnextchar "\LWRabsorbquotenumber \LWRabsorbnumber }\) \(\def
\mathcode #1={\mathchar }\) \(\let \delcode \mathcode \) \(\let
\delimiter \mathchar \) \(\def \oe {\unicode {x0153}}\) \(\def \OE
{\unicode {x0152}}\) \(\def \ae {\unicode {x00E6}}\) \(\def \AE
{\unicode {x00C6}}\) \(\def \aa {\unicode {x00E5}}\) \(\def \AA
{\unicode {x00C5}}\) \(\def \o {\unicode {x00F8}}\) \(\def \O
{\unicode {x00D8}}\) \(\def \l {\unicode {x0142}}\) \(\def \L
{\unicode {x0141}}\) \(\def \ss {\unicode {x00DF}}\) \(\def \SS
{\unicode {x1E9E}}\) \(\def \dag {\unicode {x2020}}\) \(\def \ddag
{\unicode {x2021}}\) \(\def \P {\unicode {x00B6}}\) \(\def
\copyright {\unicode {x00A9}}\) \(\def \pounds {\unicode {x00A3}}\)
\(\let \LWRref \ref \) \(\renewcommand {\ref }{\ifstar \LWRref
\LWRref }\) \( \newcommand {\multicolumn }[3]{#3}\) \(\require
{textcomp}\) \( \newcommand {\meta }[1]{\langle \textit {#1}\rangle
} \) \(\newcommand {\toprule }[1][]{\hline }\) \(\let \midrule
\toprule \) \(\let \bottomrule \toprule \) \(\def
\LWRbooktabscmidruleparen (#1)#2{}\) \(\newcommand
{\LWRbooktabscmidrulenoparen }[1]{}\) \(\newcommand {\cmidrule
}[1][]{\ifnextchar (\LWRbooktabscmidruleparen
\LWRbooktabscmidrulenoparen }\) \(\newcommand {\morecmidrules }{}\)
\(\newcommand {\specialrule }[3]{\hline }\) \(\newcommand
{\addlinespace }[1][]{}\) \(\require {colortbl}\) \(\let
\LWRorigcolumncolor \columncolor \) \(\renewcommand {\columncolor
}[2][named]{\LWRorigcolumncolor [#1]{#2}\LWRabsorbtwooptions }\)
\(\let \LWRorigrowcolor \rowcolor \) \(\renewcommand {\rowcolor
}[2][named]{\LWRorigrowcolor [#1]{#2}\LWRabsorbtwooptions }\) \(\let
\LWRorigcellcolor \cellcolor \) \(\renewcommand {\cellcolor
}[2][named]{\LWRorigcellcolor [#1]{#2}\LWRabsorbtwooptions }\)
\(\newcommand {\intertext }[1]{\text {#1}\notag \\}\) \(\let \Hat
\hat \) \(\let \Check \check \) \(\let \Tilde \tilde \) \(\let
\Acute \acute \) \(\let \Grave \grave \) \(\let \Dot \dot \) \(\let
\Ddot \ddot \) \(\let \Breve \breve \) \(\let \Bar \bar \) \(\let
\Vec \vec \) \(\newcommand {\nicefrac }[3][]{\mathinner
{{}^{#2}\!/\!_{#3}}}\)
8.2 Importing From
Matlab
8.2.1 Importing 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
copy
[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
copy
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
\addplot 3[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
copy
XX=X'; YY=Y'; ZZ=Z';
data = [ XX(:) YY(:) ZZ(:) ]
save P.dat data -ASCII
with pgfplots command
\addplot 3[surf,mesh/cols =10,mesh/ordering =rowwise,shader =interp]
file {P.dat}; .
8.2.2 matlab2pgfplots.m¶
This is a Matlab® script which attempts to convert a Matlab
figure to pgfplots . It requires Matlab
version 7.4 (or higher).
Attention:
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
La Te X
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.3 matlab2pgfplots.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.4 Importing 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 :
copy
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') ]