Write and format table to csv file

4 visualizaciones (últimos 30 días)
Alessandro Togni
Alessandro Togni el 12 de Abr. de 2021
Respondida: Rik el 13 de Abr. de 2021
Hi,
my task is to append an existent table to an already created csv file. I've managed to write the values of the table, but:
  • the header (VariableNames property of the table) does not appears on the txt file;
  • to make the reading easy i would like to obtain a formatting like this:
Var1 Var2 Var3 Var4
_____ __________ _____ _____
0.7 {'Pan' } 10 376
0.8 {'Round' } 13.59 502
1 {'Button'} 10.5 465
1.25 {'Pan' } 12 1091
1.5 {'Round' } 16.69 562
with the widht of the colums automatically tuned to data.
This is what i'm doing:
table_cfr=table(src1.Channel, src1{:,2} - src2{:,2}, src1{:,3} - src2{:,3}, src1{:,4} - src2{:,4}, src1{:,5} - src2{:,5}, src1{:,6} - src2{:,6}, src1{:,7} - src2{:,7}, src1{:,8} - src2{:,8});
table_cfr.Properties.VariableNames = {'Channel','Delta95thPerc','Delta','x1_sigma','SD95thPerc','SD','SDRatio','SDratioSD'};
writetable(table_cfr, 'validation_results.txt', 'WriteMode','append', "Delimiter", 'tab');
obtaining something like this:
C 0 0.0017 0.0002 0 0.0006 0 0
h 2.91000000000001 -17.1423 0.0111 0 -0.623 0 0
XLD1 0 -0.0035 -0.002 0 0.0018 0 0
ZRD1 -0.0400000000000063 0.0161 0.0074 0.0799999999999983 0.0006 0 0
ZRD2 0 0.1312 0.061 0 0.0474 0 0
ZRD3 NaN NaN NaN NaN NaN NaN NaN
ZLD1 -0.0400000000000063 0.0167 0.0075 -0.189999999999998 -0.000100000000000001 0 0
ZLD2 -0.0400000000000063 0.1298 0.0608 0 0.0459 0 0
ZLD3 NaN NaN NaN NaN NaN NaN NaN
YRD1 -0.120000000000005 0.0103 0.0043 -0.449999999999989 0.0016 0 0
YRD2 -0.269999999999996 0.11 0.0473 0 0.0359 0 0
YRD3 NaN NaN NaN NaN NaN NaN NaN
YLD1 -0.109999999999999 0.0082 0.004 -0.449999999999989 -0.0031 0 0
YLD2 -0.269999999999996 0.109 0.0476 0 0.045 0 0
YLD3 NaN NaN NaN NaN NaN NaN NaN
G -0.0100000000000051 0.0418 0.0126 0 0.0204 0 0
GX1 0 -0.0445 -0.0077 0 0.0012 0 0
GX2 0.0100000000000051 -0.1333 -0.0157 0.310000000000002 -0.000200000000000006 0 0
GX3 0.0999999999999943 -0.1754 -0.0166 1.5 -0.0045 0 0
Speed 0 -0.465599999999998 -0.8933 0 1.9213 0 0
Thanks in advance for the time you'll spend to read my question.

Respuestas (1)

Rik
Rik el 13 de Abr. de 2021
You can use formattedDisplayText (which was introduced in R2021a). (see this highlight for an extra example)
For older releases the best solution would be to use fprintf.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by