Convert CSV to TXT without changing data properties
37 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Jake
le 12 Mar 2020
Commenté : Walter Roberson
le 12 Mar 2020
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/276724/image.png)
How can I convert the csv without changing the actual value and keeping the precise decimal points (comma delimited)?
Thanks in Advance!
3 commentaires
Walter Roberson
le 12 Mar 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.
Réponse acceptée
Walter Roberson
le 12 Mar 2020
You do what I already told you to do, and even provided code details for: manipulate the file as text instead of as numeric.
I guarantee you that if you treat the file as numeric that you cannot meet your goal of preserving the original number of decimal places -- not unless each different column has its own fixed number of decimal places. Which I can see from your sample is not the case.
https://www.mathworks.com/matlabcentral/answers/510021-read-and-write-csv-files-without-changing-properties#answer_419381
Plus de réponses (1)
Piyush Lakhani
le 12 Mar 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 commentaires
Walter Roberson
le 12 Mar 2020
content=sprintf('%.0f, %.0f, %.1f, %.1f, %.1f, %.6f, %.6f \n',x.');
to deal with multiple rows
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!