How can I export x, y and z matrix to .xyz format file?

45 views (last 30 days)
Hello everyone
Sincerely asking.
I have x,y and z matrix which the value of every (x,y,z) represent the location of a point. I can use "surf" to export a 3D picture. Now I want to know how can I export my x,y and z matrix to a .xyz fomat file. Please help me. Thank you!
with best wishes
  7 Comments
Z G
Z G on 17 Dec 2019
For attaching the original file, I add ".txt' suffix to the original file that gwyddion can deal with. I want to export my matrix as original that can be dealed with by Gwyddion.
Guillaume
Guillaume on 17 Dec 2019
The file you've attached contains a lot more than just x, y, z coordinates of points. It looks like it starts by an extensive text header. So you first need to know which of the header information, if any, is required and you'd need to supply that required information.
The header is then followed by some binary data, possibly the x, y, z coordinates. In order to even stand a chance to understand how the data is encoded as binary we would need the exact values that have been encoded in the file, and even then it would probably required a lot of work to reverse engineer. For this kind of file, the only reliable way to know how to create them is to have the specs of the format from whomever designed it.
I want to make it clear that a file extension on its own is meaningless, it doesn't necessarily represents what is actually in the file. You can slap a .xyz extension on any file and it doesn't mean that your software will be able to read it. For example, the answer you've accepted slaps a .xyz extension onto a file that contains tab delimited data but if your software expects a file in the format you've attached it will absolutely no idea what to do with that tab delimited file. The answer could be trivially modified to put a .xlsx extension onto the file, it wouldn't mean that excel could read it.

Sign in to comment.

Accepted Answer

KSSV
KSSV on 17 Dec 2019
Let X, Y, Z be your m, n matrices.
x = X(:) ;
y = Y(:) ;
z - Z(:) ;
p = [x y z] ;
fid = fopen('test.xyz', 'wt');
fprintf(fid, [repmat('%.f\t', 1, size(P,2)-1) '%f\n'],P.');
fclose(fid)
  4 Comments
Z G
Z G on 17 Dec 2019
Though I still can't deal with the data in Gwyddion, I successfully expot matix to .xyz file from Matlab and import the file into Gwyddion by your code. So, I think it is enough.
On the way to solving the problem, every little progress deserves to be recognized. Even if that didn't end the problem, it hlep me to adjust direction.
Thank you.
Guillaume
Guillaume on 17 Dec 2019
This exports the data as a tab delimited text file. It puts a .xyz extension to that file, it doesn't mean that's what expected by your software at all. It could have put a .xlsx extension onto it, it wouldn't mean that's a file that excel can read.

Sign in to comment.

More Answers (0)

Tags

Products

Community Treasure Hunt

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

Start Hunting!