MATLAB Answers

Save generated points in txt file

10 views (last 30 days)
Ste Br
Ste Br on 20 Feb 2021
Answered: dpb on 20 Feb 2021
Hi,
I try to generate surface points and store them into txt file for further processing.
The points should be used to generate a surface in Ansys Design modeler.
Therefore the format should be in txt file like:
x1 y1 z1
x2 y2 z2
... ... ...
xn yn zn
but my code gives
x1 x2 x3 y1 y2 y3 z1 z2 z3
N = [3 3]; % size in pixels of image
F = 3; % frequency-filter width
[X,Y] = ndgrid(1:N(1),1:N(2));
i = min(X-1,N(1)-X+1);
j = min(Y-1,N(2)-Y+1);
H = exp(-.5*(i.^2+j.^2)/F^2);
Z = real(ifft2(H.*fft2(randn(N))));
surf(X,Y,Z,'edgecolor','none');
light;
X
T=table(X,Y,Z,'VariableNames',{'X','Y','Z'})
writetable(T,'MyFile.txt')
I don't know how to aligne the coordinates in a right way.
Best regards
Steff

Answers (1)

dpb
dpb on 20 Feb 2021
ndgrid generates X,Y as arrays; in your case 3x3. For output you need to convert to vectors before making the table...
T=table(X(:),Y(:),Z(:),'VariableNames',{'X','Y','Z'});
If the only point is to get the output file, you could dispense with creating the table:
writematrix([X(:),Y(:),Z(:)],'T.txt')

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!