Convert CSV to TXT without changing data properties

31 ビュー (過去 30 日間)
Jake
Jake 2020 年 3 月 12 日
コメント済み: Walter Roberson 2020 年 3 月 12 日
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 件のコメント
Jake
Jake 2020 年 3 月 12 日
Well. CSV file stores data as shown above. When I choose a cell, the exact value is shown above but the rows store data as powers. (ex: 3.51E+01, while the value is 35.11967)
When I change the extension, txt file shows data as I showed in the question :(
I'm trying MATLAB because I might have to deal with more than one file that has a lot of data.
Walter Roberson
Walter Roberson 2020 年 3 月 12 日
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.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2020 年 3 月 12 日
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
  1 件のコメント
Jake
Jake 2020 年 3 月 12 日
編集済み: Jake 2020 年 3 月 12 日
That question was not entirely related to this but this is absolutely true! I should've been more cautious because your approach is much better than what I was trying.
Thanks a lot, Walter!
#TIL with the experts here is always amazing.

サインインしてコメントする。

その他の回答 (1 件)

Piyush Lakhani
Piyush Lakhani 2020 年 3 月 12 日
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 件のコメント
Jake
Jake 2020 年 3 月 12 日
編集済み: Jake 2020 年 3 月 12 日
Turns out that the code only works for one line(row) of data :)
As Walter mentioned, "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". In my case, however, I know the field width so I can adjust the code based on that. which is the following part.
content=sprintf('%f, %f, %f, %f, %f, %f, %f \n',x);
as,
content=sprintf('%.0f, %.0f, %.1f, %.1f, %.1f, %.6f, %.6f \n',x);
But, it doesn't work as desired when the number of rows are more than one. (I'm attaching the sample file for anyone's reference)
Thank you very much for your time.
Walter Roberson
Walter Roberson 2020 年 3 月 12 日
content=sprintf('%.0f, %.0f, %.1f, %.1f, %.1f, %.6f, %.6f \n',x.');
to deal with multiple rows

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by