# Exporting data from a Plot3D

In Maple, I can get data from a plot3d like this,

restart;
Eq1:=diff(u(eta,t),t)=diff(u(eta,t),eta\$2);
BCs := {u(0,t)=sin(t), u(10,t)=0};
ICs := {u(eta,0)=0};
sys:={Eq1};
pds:= pdsolve(sys,ICs union BCs,numeric,time=t,range=0..10);
p1:= pds:-plot3d(u(eta,t),t=0..35,eta=0..10,grid=[360,360]):


Here is the command for exporting data from plot p1

A:= op([1,3],p1):
a,b:= op(op([1,1],p1)):
c,d:= op(op([1,2],p1)):
m:= rhs(rtable_dims(A)[1]):
n:= rhs(rtable_dims(A)[2]):
for i from 1 to m do
for j from 1 to n do
X:= ((m-i)*a + (i-1)*b)/(m-1);
Y:= ((n-j)*c + (j-1)*d)/(n-1);
fprintf("C:/tcdata/SL1.txt",
"%f  %f  %f\n",Y,X,A[i,j]);
end do
end do:
close("C:/tcdata/SL1.txt"):


This gives me data in the desired format, which I can later use in another software. The format is

0.000000  0.000000  0.000000
0.097493  0.000000  0.102694
0.194986  0.000000  0.200925
0.292479  0.000000  0.294693
0.389972  0.000000  0.383998
0.487465  0.000000  0.468841
0.584958  0.000000  0.549221
0.682451  0.000000  0.625138
0.779944  0.000000  0.696593
0.877437  0.000000  0.763584
..............


How can I get data from Plot3D in the same format (see above) in Mathematica?

ClearAll["Global*"];

pdeset = {Derivative[1, 0][U][t, x] == Derivative[0, 2][U][t, x]}

ics = {U[0, x] == 0};

bcs = {U[t, 0] == Sin[t], U[t, 10] == 0};

bcAll = Flatten[{ics, bcs}, 1];

sol = NDSolve[{pdeset, bcAll}, {U}, {t, 0, 35}, {x, 0, 10}];

Plot3D[(U /. First@sol)[t, x],  {t, 0, 35}, {x, 0, 10},
PlotRange -> All]

• It isn't clear to me whether you want to export a table of values from Mathematica (as you did in your Maple code) or a 3D plot as an image. Please clarify. Feb 11, 2018 at 7:04
• @m_goldberg I want to export data values from Plot3D in Mathematica as I did in Maple.
– zhk
Feb 11, 2018 at 7:28
• Export["data.txt", output, "Table"];
– mrz
Feb 11, 2018 at 7:37
• @mrz What exactly we are exporting here?
– zhk
Feb 11, 2018 at 7:38

No need to extract the points from the plot as you have the generating function already, unless this is exactly what you want

Export["pts-plot.dat", Plot3D[(U /. First@sol)[t, x], {t, 0, 35}, {x, 0, 10},
PlotRange -> All][[1, 1]], "Table"]


Otherwise, use Table or its parallel version to obtain as many points as you want:

pts = Flatten[ParallelTable[{t, x, (U /. First@sol)[t, x]}, {t, 0, 35, 0.5}, {x,0, 10, 0.5}], 1]:
Export["pts.dat", pts, "Table"]

• Thanks, I learned "indexing" the Plot-object to get the points. tricky! Feb 11, 2018 at 14:50
• @UlrichNeumann Yes, very useful to access different features of Plot-object Feb 11, 2018 at 15:35
• Both approaches worked just fine for me. The first one is what I was looking for. Thx
– zhk
Feb 12, 2018 at 4:55

pic=Plot3D[(U /. First@sol)[t, x],  {t, 0, 35}, {x, 0, 10},PlotRange -> All]

p=Flatten[Cases[bild, _GraphicsComplex , Infinity][[;; , 1]], 1]

Export["data.txt", p, "Table"]
`