writetable 関数で、数値をファイル出力する際、各列の数値桁数を任意に設定できますか?
20 ビュー (過去 30 日間)
古いコメントを表示
MathWorks Support Team
2021 年 9 月 8 日
回答済み: MathWorks Support Team
2021 年 12 月 24 日
writetable 関数で、数値をファイル出力する際、各列の数値桁数を任意に設定する方法を教えてください。
例えば、下記 table データにおいて、A 列は、下 1 桁、B 列は 下 3 桁、のように設定したいです。
>> data = [(1:3)', rand(3,1)];
>> T = array2table(data,'VariableName',{'A','B'})
T =
A B
_ _______
1 0.81472
2 0.90579
3 0.12699
>> writetable(T,'mydata.csv','WriteRowNames',true)
採用された回答
MathWorks Support Team
2021 年 9 月 8 日
writetable 関数では、数値データは long g 形式で出力され、任意の桁数で出力することはできません。
代替案として、num2str関数などで、数値を任意桁数の文字列に変換する方法があります。
>> T.A = num2str(T.A,'%1.1f\n');
>> T.B = num2str(T.B,'%1.3f\n');
>> T
T =
A B
___ _____
1.0 0.815
2.0 0.906
3.0 0.127
>> writetable(T,'mydata.csv','WriteRowNames',true)
>> type mydata.csv
A,B
1.0,0.815
2.0,0.906
3.0,0.127
0 件のコメント
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!