フィルターのクリア

How to save numerical array as file (any format) with different precisions (format) for different columns in the same array?

4 ビュー (過去 30 日間)
Hi I am matlab 2016b user. I have one numerical array (950*2 double). When I save this, I want to save 1st column with integer format (no decimal point...I mean %.0f) and to save 2nd column with scientific notation with 16 sigifinicant digits....such as '4.123456789012345e-11'. This is important issue with processing speed in my code. So far, when I use .mat format (binary) with save' function, it gave 9 kb in file size. However, I may need to use no-binary format like...txt....csv...etc. When I tried fprintf and dlmwrite functions with diverse options, 26 kb was record with txt/csv format. I also saw some similar answers using loop for fprintf function, but regarding code running time issue, I hope I can hear another answer using for loop.
Do you know how to do it?
  3 件のコメント
Deo
Deo 2017 年 3 月 30 日
I need to improve file reading and writing speed by reducing file size. However, for communication and file exchange/share with other person who does not use Matlab, I may need to use txt/csv file. For example, if I just use '-ascii' option, it give 8 digits precision and these are useless for first column and not enough for 2nd column.
Because my two codes generating and reading 6000000 files, so even reducing 1 kb is important for me as total calculation time.
Sorry for my english.
Walter Roberson
Walter Roberson 2017 年 3 月 30 日
Consider sending the files in binary, but also providing the person with a program that allows them to view the binary as text. With 6 million files, you can be sure that the other person is not going to read the text of more than a very small fraction of them.

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

採用された回答

Guillaume
Guillaume 2017 年 3 月 30 日
One simple way to write that matrix as text according to your specifications:
x = rand(950, 2) .* [100, 1]; %demo data, requires R2016b or later
fid = fopen('somename.csv', 'wt');
fprintf(fid, '%0.f, %.16e\n', x.');
fclose(fid);
  1 件のコメント
Deo
Deo 2017 年 3 月 30 日
編集済み: Deo 2017 年 3 月 30 日
Thanks I got this way to switch column and row from other q/a, and this way was what I got 26 kb for each file. I may need to find way to reduce some...useless file information, etc. Thanks for your help :D

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 3 月 30 日
fid = fopen('YourOutputFile.csv', 'wt');
fmt = '%d, %.21e\n';
data = [round(YourArray(:,1)), YourArray(:,2)];
fprintf(fid, fmt, data .'); %transpose is important here
fclose(fid)
The text output will definitely be larger than binary.

カテゴリ

Help Center および File ExchangeLow-Level File I/O についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by