Table formatting problem with cell and array matrix
3 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Voss
on 8 Nov 2023
% 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
7 Comments
Voss
on 8 Nov 2023
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.
More Answers (0)
See Also
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!