how to save as .csv

10 次查看(过去 30 天)
Ferda
Ferda 2016-2-16
I have a "<512x34 double>" file. you can see the part of it in the picture. I would like to write this data into a .csv file in a way that the numbers' decimals look exactly the same. I tried doing this with excell but decimals were converted into comma. I need the decimals to be dot. how can I do it ?

回答(2 个)

Walter Roberson
Walter Roberson 2016-2-16
The format of your column 11 is not consistent with the other columns. In order to get the decimals exactly like you show there, you will need to write the data to file yourself instead of using one of the provided tools.
numfield = size(YourData, 2);
fmt_cell = repmat({'%.4f', 1, numfield);
fmt_cell{11} = '%.4e'; %do this for all other columns that need the exponential format
fmt = [strjoin(fmt_cell, ','), '\n'];
fid = fopen('YourOutputFile.csv', 'wt');
fprintf(fid, fmt, YourData.' ); %the .' is needed!
fclose(fid);
If you want the exponential format with the 'e' numbers to show up depending upon the value of the individual location rather than upon which column it is in, then that is possible but requires more coding; you would need to indicate the rules you want to follow for us to be able to suggest appropriate code.

Image Analyst
Image Analyst 2016-2-16
So you say Excel turned your decimals into commas when writing it out from Excel to a csv file? That's weird. What is your country/region in your operating system? Some countries use commas for decimal points - are you sure you're not set up to be one of those countries? Even if you were, it's weird that it showed up in Excel as points and the csv text file as commas - that's a discrepancy. Anyway, enough about Excel -- did you try it in MATLAB with csvwrite() or dlmwrite()?
  2 个评论
Walter Roberson
Walter Roberson 2016-2-16
csvwrite() will not preserve the desired precision. dlmwrite() can be passed a Precision option, but it would use that precision (or format) for all cells, and there is not one single format that will switch to exponential notation as low as E+03
(%g will not switch to exponential until the number of digits before the decimal point exceeds the specified field width, and since the user wants 4 digits after the decimal point, %g format would not switch until E+04)
Image Analyst
Image Analyst 2016-2-16
Well at least it shouldn't convert decimal points into commas.

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by