Preserve precision when writing array to csv?

Hi, I have a simple script that converts geographic data in polar cylindrical coordinates to decimal degrees. The output is a very large 3 columns by ~1.5 million rows array to 4 decimals precision. This inconvenient format is necessary because of the software (ArcMap) the output csv needs to be imported into.
First I tried this:
format long g
csvwrite(fileout,data);
fclose('all')
The result imports perfectly, but the longitude values (the first array column) are truncated to 2 decimal places.
Then I tried this, referencing the question here:
dlmwrite(fileout, data, 'delimiter', ',', 'precision', 7);
fclose('all')
The output looks perfect in both matlab and excel, but is missing the second column upon import into the other software. Because of the large number of rows, I can't edit or resave in excel to try to fix whatever this problem is.
Finally I tried simply
writematrix(data, fileout)
fclose('all')
but the lines run, proceeds to completion very quickly without error, and does not write a file.
I'm out of ideas. Am I missing an alternative here, or is there something in my dlmwrite line that might have caused problems?
Thanks!

4 Kommentare

Stephen23
Stephen23 am 17 Jul. 2019
Bearbeitet: Stephen23 am 17 Jul. 2019
"..but the longitude values (the first array column) are appended to 2 decimal places."
What does this mean? The word "appended" means to join something on the end of something else... so do you get two extra digits concatenated onto the end of the printed values?
Or do you actually mean "truncated" (i.e. shortened)? Or simply that the values are printed with two decimal places? Maybe an example would help.
It is not clear what you expect that format operator to achieve, but it is worth reading the csvwrite documentation, which states "csvwrite writes a maximum of five significant digits. If you need greater precision, use dlmwrite with a precision argument."
Note that none of your fclose calls are required or do anything relevant to those functions: none of those functions use an explicit fopen, and so none of them require an explicit fclose.
"...but is missing the second column upon import into the other software."
Taking a wild guess, but it sounds like the "other software" requires a trailing delimiter character. The simplest way to achieve that is also the most efficient: write your own file using low-level fprintf. Can you please upload a sample file that works with this "other software" ?
Jeff Leeburn
Jeff Leeburn am 17 Jul. 2019
Yes, I meant truncated, sorry about that. And I see, so csvwrite isn't useful for my purposes. Thank you for your help.
Jeff Leeburn
Jeff Leeburn am 17 Jul. 2019
The other software is ArcGIS 10.6, and I'll have to look into a trailing delimiter character. Attached is an output example from csvwrite that imports fine but lacks the last two decimals of precision in the first column.
Thanks for your help, I appreciate it!
Guillaume
Guillaume am 17 Jul. 2019
There is nothing wrong with your dlmwrite call, so the problem is with ArcGIS. Maybe the software documentation can enlighten you as to what format it expects.
writematrix(data, fileout) should write a file or error. Are you sure you're looking in the right folder for the file? The simplest way to avoid confusion over which folder the file is written in is to use absolute paths.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Produkte

Version

R2019a

Gefragt:

am 17 Jul. 2019

Kommentiert:

am 17 Jul. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by