Reputation: 445
I have an array which looks like this:
cloud =
7.5059 51.4406
7.5057 51.4445
7.5048 51.4484
7.5034 51.4522
7.5014 51.4558
7.4989 51.4593
7.4958 51.4627
7.4923 51.4658
7.4884 51.4686
.
.
all i want is to write this array to a text file as it is, in the same format. I tried both fprintf
and dlmwrite
but i'm able to produce the exact same format. I know its an easy one, but I'm only asking after trying a lot.
Upvotes: 2
Views: 381
Reputation: 114786
Have you looked into string formatting?
fid = fopen( 'myFile.txt', 'w' );
for ii=1:size(cloud,1)
fprintf( fid, '%.5g\t%.5g\r\n', cloud(ii,1), cloud(ii,2) );
end
fclose( fid ); % do not forget to close the file :-)
Have you considered save
ing into ascii file?
save( 'myFile.txt', 'cloud', '-ascii', '-tabs' );
EDIT:
End-of-line issue: for text file there are several way of marking the end of line: On windows it is usually required to print \r\n
, for Mac and Linux sometimes it is enough to use \r
and sometimes \n
(I'm not 100% sure). So, you might need to experiment with it a bit to find out what works best for your machine. (Thanks @Rody for correcting me here)
Accuracy: the number in the formatting string %.5g
determines the accuracy of the printed number. Again, you can play with it till you are satisfied with the results.
Upvotes: 3
Reputation: 38032
Here's one way:
fid = fopen('cloud.txt', 'w');
fprintf(fid, '%.4f\t%.4f\r\n', cloud.');
fclose(fid)
Here's the more readable way:
dlmwrite('cloud.txt', cloud, 'delimiter', '\t', 'precision', 4, 'newline', 'pc')
Here's one way:
fid = fopen('cloud.txt', 'w');
fprintf(fid, '%.4f\t%.4f\n', cloud.');
fclose(fid)
Here's the more readable way:
dlmwrite('cloud.txt', cloud, 'delimiter', '\t', 'precision', 4);
Upvotes: 2