x=-2:.1:2;
y=x;
[X,Y]=meshgrid(x,y);
Z=exp(-X.^2-Y.^2);
n=length(x);

v=0*ones(n*n,3);
T=0*ones(2*n*n-2*n,3);
for i=1:n;
    for j=1:n;
        ind=j+(i-1)*n;
        v(ind,:)=[X(i,j),Y(i,j),Z(i,j)];
    end
end
for i=1:n-1;
    for j=1:n;
        ind=j+(i-1)*n;
        ind2=j+i*n;
        indt=2*ind-1;
        indt2=2*ind;
        T(indt,:)=[ind,ind+1,ind2];
        T(indt2,:)=[ind+1,ind2+1,ind2];
    end
end
T2(:,:)=T(1:end-1,:);

TR=triangulation(T2,v);

stlwrite(TR,'testfile.stl','text');