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

20 次查看(过去 30 天)
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
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 CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2015b

Community Treasure Hunt

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

Start Hunting!