MATLAB: How to convert matrices into an xyz file

csvmatrixxyz

Hi,
I have several matrix files: one with x coordinates, one with y coordinates, and one with z values (wave energies). I am wondering how I can convert these into a csv file with 3 columns (for x, y and z – i.e. an xyz file)?
I am totally new to Matlab (sorry!) and any help would be greatly appreciated 🙂 Thanks in advance!

Best Answer

See the documentation for the csvwrite (or xlswrite) function to write the file.
To combine your vectors into one matrix, you can use bracket concatenation with [], or the cat funciton. The bracket concatenation is easier for your problem:
xyz = [x(:), y(:), z(:)];
The ‘(:)’ creates each as a column vector, so providing they are the same length, that should work as written.