writetable 関数で、数値をファイ​ル出力する際、各列の​数値桁数を任意に設定​できますか?

20 ビュー (過去 30 日間)
MathWorks Support Team
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
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 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2015b

Community Treasure Hunt

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

Start Hunting!