MATLAB: Does output that writing to a file is not the same as its value

MATLABtext file

I am trying to print the xyz coordinate value of the pointcloud data to a file. I need 3 columns in the file as indicate the x, y, and z value for each column.
This is the code that I import the pointcloud.
ptCloud = pcread ('pointcloud.ply');
After that I retrieve the xyz coordinate of the pointcloud and display it.
xyz = ptCloud.Location;
display (xyz);
The result is as follow:
So, I print it out to a text file.
file = fopen ('xyzCoordinate.txt', 'wt');
fprintf (file, '%f\n', xyz);
fclose (file);
However, there is only one column in the text file that not what I want.
What is the problem? I have no idea what mistake I have made, please help!

Best Answer

Your format string '%f\n' will print exactly one number on each line. If you want multiple number per line then you need to specify this using the format string. Because MATLAB works down the columns first you will also need to transpose the matrix. Both of these are shown here, with the addition of assert to check if the file was opened properly:
[fid,msg] = fopen('xyzCoordinate.txt', 'wt');
assert(fid>=3,msg)
fprintf(fid, '%f\t%f\t%f\n', xyz.');
fclose(fid)
Standard practice in MATLAB does not include a space between the function name and the parentheses.