Convert CSV to TXT without changing data properties
Ältere Kommentare anzeigen
Hello,
I have a CSV file that contains random values, all of them being decimals. Some of them have one decimal value while others have 6 or 7 decimals. When I convert them to txt, values are stored as follows.

How can I convert the csv without changing the actual value and keeping the precise decimal points (comma delimited)?
Thanks in Advance!
3 Kommentare
Stephen23
am 12 Mär. 2020
Change the file extension.
Jake
am 12 Mär. 2020
Walter Roberson
am 12 Mär. 2020
Csv does not store as scientific notation and pop up to full decimal places. However, Excel does that, and in MATLAB if you view the variable with the variable browser matlab does that unless you set the variable browser to use g format. Preferences can change the default browser format.
Akzeptierte Antwort
Weitere Antworten (1)
Piyush Lakhani
am 12 Mär. 2020
Hi James,
Following way might be work for you.
x=csvread('filename.csv','Row_offset','column_offset'); %if your file has text then give offset for column or row
content=sprintf('%f, %f, %f, %f, %f, %f, %f \n',x); % put %f number of times same as number of columns,
% Insted of %f you can set the decimals you wants like eg. %5.6f
fId='file.txt'
fId = fopen(fId, 'w') ;
fwrite( fId, content ) ;
fclose( fId ) ;
5 Kommentare
Jake
am 12 Mär. 2020
Walter Roberson
am 12 Mär. 2020
No this does not preserve decimal places!! %f format defaults to %.6f which gives exactly 6 places after the decimal. Yes you can adjust field by field but you would have to know the field width ahead of time and it would have to be consistent for any one column. However, it isn't. In particular, the original csv has trailing 0 suppression on so for example 1.2300 appears in the file as 1.23 even if other entries in the same column use 4 decimal places. But James wants to preserve decimal places exactly as in the original file, so if he knew that a column had up to 4 decimal places, it would not be acceptable to him to output all the values in that column with exactly 4 decimal places: he wants the entry with two decimal places to show up with 2 decimal places.
The only effective way to handle this is to treat the input as text so that you can handle the varying number of decimal places and you can handle entries that end in 0 that is considered significant and must be preserved.
It is possible that have files in which each entry in a column has a different number of decimal places and trailing 0 that are in the data are important. most files can be handled through a fixed number of decimal places for any given column, but sometimes the number of trailing 0s is important. Not often; thinking of them as being important is much more often a mistake of interpretation.
One case where the number of training zeros is important is in expressing physical constants, in which case the number of digits written gives information about the precision. 2.5400 for a constant gives different information than 2.54 does.
Another case where trailing 0 is important is in some expressions of binary fractions. For example 101.11100 provides information about the number of bits being used in a way that 101.111 would not.
But as I said earlier, more often insisting on preservation of decimal places is a mistake compared to using a fixed number of decimal places (either total or for the fraction)
Jake
am 12 Mär. 2020
Walter Roberson
am 12 Mär. 2020
content=sprintf('%.0f, %.0f, %.1f, %.1f, %.1f, %.6f, %.6f \n',x.');
to deal with multiple rows
Kategorien
Mehr zu Data Type Conversion finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
