フィルターのクリア

How to use fwrite with a format subject to change

1 回表示 (過去 30 日間)
012786534
012786534 2020 年 12 月 7 日
コメント済み: 012786534 2020 年 12 月 7 日
Hi,
I have a basic question: I am doing multiple SQL queries and exporting the result to text files. I want to use fwrite to export the data but the number of variables and the data types are liable to change between queries. How could I build a script sufficiently flexible to handle that ?
var_a = {'T1','T1','T1','T1'}';
var_b = [2,2,2,2]';
var_c = {'A','A','A','A'}';
var_d = [8.12,7.67,5.90,3.81]';
t = table(var_a, var_b, var_c, var_d)
file_format = ['%s %d %s %f\n'];
y = table2cell(t).';
fid = fopen('test.txt', 'wt');
fprintf(fid, file_format, y{:});
fclose(fid);
Thank you,

回答 (1 件)

Steven Lord
Steven Lord 2020 年 12 月 7 日
Since you're already storing your data in a table array, why not just use writetable?
  1 件のコメント
012786534
012786534 2020 年 12 月 7 日
Because things like writetable and writecell are really slow and fwrite is much faster. This matters depending on the number of SQL queries and the size of the files

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

カテゴリ

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