Table formatting problem with cell and array matrix

1 次查看(过去 30 天)
In relation to my previous post with a different layout.
How might i re-arrange the script to produce the following basic layout?
species max frac mix-ratio
*H2 9.6220083e-01 3.0000000e-01
H2O 8.9880156e-01 5.0000000e-01
*O2 1.0974098e-01 7.5000000e+00
H2O(S) 4.9605364e-02 8.0000000e+00
*OH 3.2755249e-02 9.0000000e+00
*H 7.3249660e-03 9.0000000e+00
*O 3.2515351e-03 1.0000000e+01
HO2 2.0230744e-05 1.0000000e+01
H2O2 1.6082207e-06 1.0000000e+01
O3 9.8114188e-09 1.0000000e+01
The first row is simply header titles above variables listed in columns.
I can do it with fprintf, but would also like to do it with table and array2table in MATLAB 2017a. Part way there but i cannot bring it all together!
If i could also duplicate the header titles via 'VariableNames', that would be greatly appreciated.

采纳的回答

Voss
Voss 2023-11-8
% inputs
pnamesNew ={'*H2';'H2O';'*O2';'H2O(S)';'*OH';'*H';'*O';'HO2';'H2O2';'O3'};
maxFracsNew = [9.6220e-01;8.9880e-01;1.0974e-01;4.9605e-02;3.2755e-02;7.3250e-03;3.2515e-03;2.0231e-05;1.6082e-06;9.8114e-09];
of = [3.0000e-01;5.0000e-01;7.5000e+00;8.0000e+00;9.0000e+00;9.0000e+00;1.0000e+01;1.0000e+01;1.0000e+01;1.0000e+01];
% create the table:
T = table(pnamesNew,maxFracsNew,of,'VariableNames',{'species', 'max frac', 'mix-ratio'});
% write the table to file:
writetable(T,'output.txt','Delimiter','\t')
% check the contents of the file:
type output.txt
species max frac mix-ratio *H2 0.9622 0.3 H2O 0.8988 0.5 *O2 0.10974 7.5 H2O(S) 0.049605 8 *OH 0.032755 9 *H 0.007325 9 *O 0.0032515 10 HO2 2.0231e-05 10 H2O2 1.6082e-06 10 O3 9.8114e-09 10
  7 个评论
Voss
Voss 2023-11-8
You're welcome!
And just to be prefectly clear, the type() usage in my answer is just to show what's in the file for debugging/demonstration; you shouldn't need it in your code once you're sure your code does what you intend.
Brantosaurus
Brantosaurus 2023-11-8
Yes, thanks for confirming that. I found it’s convenient for diagnostic checks

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

产品


版本

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by