# exporting surface color data from MATLAB for usage within pgfplots code

I am writing a MATLAB program that exports MATLAB plots to tikz code. One of the features that I find very useful and that I would also like to implement is the specification of the surface color. In MATLAB one would usually run the code

``````surfc(X,Y,Z,C)
``````

where `[X,Y]=meshgrid(x,y)` are meshgrids of the vectors `x,y`, `Z=f(X,Y)` is some function of these meshgrids, and `C` is the surface color data. In the pgfplots manual, the author states that the data `X,Y,Z` can be exported using the code:

``````XX=X'; YY=Y'; ZZ=Z';
data = [ XX(:) YY(:) ZZ(:) ]
save P.dat data -ASCII
``````

This can be found in the pgfplots manual on page 538. However, this export neglects the surface color data found in `C`. In some simulations `C` can be something different than the typical z-axis, e.g. `C = X.*Y` (Note `.*` is the equivalent of the Hadamard product in MATLAB). Is there a way to consider this quantity in pgfplots? If so how can it be exported and integrated into the code?

In general, my question can be divided into two parts:

1. Is there a way to export `C` in order to specfiy the surface color data and set the colorbar axis? Preferably some way like this:
``````XX=X'; YY=Y'; ZZ=Z' CC = CC';
data = [ XX(:) YY(:) ZZ(:) CC(:)]
save P.dat data -ASCII
``````
1. Does pgfplots accepts this? If so, how can I use the exported data? (small example would be helpful)

To clarify what I mean with 2) I will provide a small example. When the data `X,Y,Z` are exported, one can use the code

``````\addplot3[surf,mesh/cols=10,mesh/ordering=rowwise,shader=interp] file {P.dat};.
``````

to plot the surf data. How does the above code line change if the color data is additionally exported?

Interestingly, I was able to find the answer through trial and error. For anyone interested in this type of work, all you have to do is use my suggested code:

``````XX=X'; YY=Y'; ZZ=Z' CC = CC';
data = [ XX(:) YY(:) ZZ(:) CC(:)]
save P.dat data -ASCII
``````

Then by expanding the `addPlot` command as follows:

``````\addplot3[surf,mesh/cols=10,mesh/ordering=rowwise,shader=interp, point meta = explicit] file {P.dat};
``````

one obtains the desired result, i.e. the surface is colored according to the fourth column in the exported table. The keyword `point meta = explicit` tells pgf that the table has the following structure

``````xcol    ycol    zcol     colordata
``````

Hence, pgf directly knows how to color the surface and where to find the information. Very nice feature, which is sadly not explicitly mentioned for surf plots in the pgf manual. I guess it is a feature that should be discerned by the reader.