fprintf formatting problem with cell and array matrix 2

2 views (last 30 days)
In a simular vein to a previous query.
Could somone kindly show me how to finnish off what appears to be another simple formatting problem. Nearly there, but the last step has beaten me again!
I basically need the transpose of 'ofmf.txt'. The script to produce this is 'script.m'.
Tried with 'scriptT.m', but it outputs 'ofmtT.txt'. Just a question of somehow appending the 'fracs' data to the names.
  3 Comments
Stephen23
Stephen23 on 23 Oct 2023
It is not just a trivial transpose. Here are some approaches:
  1. use a loop and indexing,
  2. use a table (recommended),
  3. use e.g. COMPOSE and display the text,
  4. convert the numeric data into a cell array, concatenate with the header, then one FPRINTF call with a comma-separated list.
Unfortunately this forum implicitly adds newlines after each FPRINTF call, so I gave up trying to give examples.
Walter Roberson
Walter Roberson on 23 Oct 2023
Unfortunately this forum implicitly adds newlines after each FPRINTF call
Not exactly -- it adds newlines at the end of each code line that does an fprintf. It also removes empty lines created by fprintf
fprintf('A'); fprintf('B')
AB
fprintf('C')
C
fprintf('\n\n\n\n')
fprintf('D')
D
disp(string(newline)+newline+newline+newline)
fprintf('E')
E
It is a nuisance sometimes.

Sign in to comment.

Accepted Answer

Voss
Voss on 23 Oct 2023
fracs = ...
[1.080799513888714E-32 0 0.62466759170135333 0.0742119108820972 0 0 0 ...
0 0 0.30112049741654717;
8.1862156579892249E-31 0 0.43700138755203366 0.56299861244797844 0 0 ...
0 0 0 0;
4.8637114770698571E-21 0 0.32440166506243995 0.67559833493756316 0 0 ...
4.2291497653345257E-26 0 0 0;
1.6450880329246443E-15 3.1789404533678006E-36 0.24933518340270872 0.7506648165972899 ...
3.0673853913761709E-28 1.3223645939372742E-32 8.0871899392903947E-19 ...
6.70013387010215E-33 0 0;
4.900395718623101E-12 6.1747782151010521E-28 0.19571626793147767 0.80428373206852888 ...
2.2269718209845535E-22 1.6572230040113729E-24 3.3433984090423735E-14 ...
9.2977078152200557E-25 0 0;
1.0257351304705146E-9 2.617799763018077E-22 0.15550208132805002 0.84449791867194646 ...
2.1781579716418966E-18 4.9208050395830782E-19 4.3857807128809733E-11 ...
3.2214171097127513E-19 0 0;
4.3103181910295979E-8 2.7279345388281687E-18 0.12422433753331349 0.87577561936350534 ...
1.570342966673278E-15 3.78000038794537E-15 7.1379146386309683E-9 2.9689871616039746E-15 ...
3.5360248709238178E-32 0;
6.5459411463843508E-7 2.7320582329382315E-15 0.099201583893637285 0.9007974509664014 ...
2.1726807488669134E-13 2.8836393026009774E-12 3.1054584734036675E-7 ...
2.7651925411612618E-12 7.12366402656099E-27 0;
5.0662163511599685E-6 5.883213268176446E-13 0.078724813502676808 0.92126444320230738 ...
1.0153013026281985E-11 4.8262185118338723E-10 5.6770786659506609E-6 ...
5.7354594102813562E-10 9.7023674645676979E-23 0;
2.4421990262674956E-5 4.403704051947919E-11 0.06164794655733434 0.93827052251646137 ...
2.2445716907411361E-10 2.8377451585746555E-8 5.7038008629661728E-5 4.25498603104255E-8 ...
2.0811584806073058E-19 0;
8.2187030635818051E-5 1.5288924079026876E-9 0.047173110175765094 0.95237302382510747 ...
2.8721600227496867E-9 7.7581284460813075E-7 0.00036939736170548689 1.5057939350703259E-6 ...
1.1726859150654507E-16 0;
0.00020427712746989916 2.9105403726253816E-8 0.034760508512691225 0.96330610874190448 ...
2.3876219545799965E-8 1.1566323656074547E-5 0.0016873015383648579 3.0208650517165084E-5 ...
2.3443091248967122E-14 0;
0.00038077633599091645 3.3138117176928766E-7 0.024151961677904805 0.96931004937101217 ...
1.3649589247496569E-7 0.00010177468680586972 0.0056759924892255493 0.00037897756200643907 ...
1.9511337609553526E-12 0;
0.00052407082677757269 2.2742164116491102E-6 0.015511740358074975 0.96632796330430382 ...
5.3611083709640121E-7 0.00053118366049772158 0.014065446275684821 0.0030367852474231624 ...
6.887405688319738E-11 0;
0.00052856823076589509 8.658464587260697E-6 0.0093989841734753513 0.9495411058272516 ...
1.3577058415442342E-6 0.0015427398356310203 0.024754647508174875 0.014223938254272516 ...
8.8099805664011839E-10 0;
0.00042813835721212761 1.8749212329738908E-5 0.0057655124991027457 0.920591265384587 ...
2.269548855159085E-6 0.0026396910954343466 0.032128828877358365 0.038425545025119787 ...
4.149228770119087E-9 0;
0.00031494793492961137 2.8327147863117975E-5 0.0037057710019004955 0.88634406267706345 ...
2.922132902838032E-6 0.0032768974022960775 0.034775463743648316 0.071551607959386754 ...
1.018411787725647E-8 0;
0.00022348304331810186 3.5251522913437423E-5 0.0024786422606670384 0.85134668127933666 ...
3.2746278191248033E-6 0.00344139271559275 0.034292082662405188 0.10817919188794742 ...
1.7500625275918122E-8 0;
0.00015605925600176512 3.9341075649633064E-5 0.001702599392503859 0.8174699691665277 ...
3.3970252161274673E-6 0.0032942135941524892 0.032135291011034732 0.14519912947891617 ...
2.4579722439036182E-8 0;
0.0001078783250984206 4.1056317609275734E-5 0.0011891055667362491 0.78536474093557762 ...
3.3613947843293898E-6 0.0029771549758717086 0.029184848667306983 0.18113182333004763 ...
3.0486964237300663E-8 0;
7.392252463130788E-5 4.0959000051015282E-5 0.00083862229703092787 0.755195725277905 ...
3.2225776517101929E-6 0.0025861369476422578 0.025940842802949327 0.2153205337688972 ...
3.4803232955347635E-8 0;
5.0210295664953338E-5 3.9555945293785451E-5 0.0005944585106563569 0.7269340589384562 ...
3.01982398381767E-6 0.0021806906182302303 0.022687255264208894 0.24751071315297202 ...
3.7450541969004565E-8 0];
of = [0.5; 1; 1.5; 2; 2.5; 3; 3.5; 4; 4.5; 5; 5.5; 6; 6.5; 7; 7.5; 8; 8.5; ...
9; 9.5; 10; 10.5; 11];
pnames = {'*H','HO2','*H2','H2O','H2O2','*O','*OH','*O2','O3','H2O(S)'};
fileID = fopen('ofmfT.txt','w');
fmt = ['%15s', repmat('%15.7e ', 1, numel(of)-1), '%15.7e\n'];
data = [[{'Mix ratio'}; num2cell(of)] [pnames; num2cell(fracs)]];
fprintf(fileID,fmt,data{:});
fclose(fileID);
% check
type('ofmfT.txt')
Mix ratio 5.0000000e-01 1.0000000e+00 1.5000000e+00 2.0000000e+00 2.5000000e+00 3.0000000e+00 3.5000000e+00 4.0000000e+00 4.5000000e+00 5.0000000e+00 5.5000000e+00 6.0000000e+00 6.5000000e+00 7.0000000e+00 7.5000000e+00 8.0000000e+00 8.5000000e+00 9.0000000e+00 9.5000000e+00 1.0000000e+01 1.0500000e+01 1.1000000e+01 *H 1.0807995e-32 8.1862157e-31 4.8637115e-21 1.6450880e-15 4.9003957e-12 1.0257351e-09 4.3103182e-08 6.5459411e-07 5.0662164e-06 2.4421990e-05 8.2187031e-05 2.0427713e-04 3.8077634e-04 5.2407083e-04 5.2856823e-04 4.2813836e-04 3.1494793e-04 2.2348304e-04 1.5605926e-04 1.0787833e-04 7.3922525e-05 5.0210296e-05 HO2 0.0000000e+00 0.0000000e+00 0.0000000e+00 3.1789405e-36 6.1747782e-28 2.6177998e-22 2.7279345e-18 2.7320582e-15 5.8832133e-13 4.4037041e-11 1.5288924e-09 2.9105404e-08 3.3138117e-07 2.2742164e-06 8.6584646e-06 1.8749212e-05 2.8327148e-05 3.5251523e-05 3.9341076e-05 4.1056318e-05 4.0959000e-05 3.9555945e-05 *H2 6.2466759e-01 4.3700139e-01 3.2440167e-01 2.4933518e-01 1.9571627e-01 1.5550208e-01 1.2422434e-01 9.9201584e-02 7.8724814e-02 6.1647947e-02 4.7173110e-02 3.4760509e-02 2.4151962e-02 1.5511740e-02 9.3989842e-03 5.7655125e-03 3.7057710e-03 2.4786423e-03 1.7025994e-03 1.1891056e-03 8.3862230e-04 5.9445851e-04 H2O 7.4211911e-02 5.6299861e-01 6.7559833e-01 7.5066482e-01 8.0428373e-01 8.4449792e-01 8.7577562e-01 9.0079745e-01 9.2126444e-01 9.3827052e-01 9.5237302e-01 9.6330611e-01 9.6931005e-01 9.6632796e-01 9.4954111e-01 9.2059127e-01 8.8634406e-01 8.5134668e-01 8.1746997e-01 7.8536474e-01 7.5519573e-01 7.2693406e-01 H2O2 0.0000000e+00 0.0000000e+00 0.0000000e+00 3.0673854e-28 2.2269718e-22 2.1781580e-18 1.5703430e-15 2.1726807e-13 1.0153013e-11 2.2445717e-10 2.8721600e-09 2.3876220e-08 1.3649589e-07 5.3611084e-07 1.3577058e-06 2.2695489e-06 2.9221329e-06 3.2746278e-06 3.3970252e-06 3.3613948e-06 3.2225777e-06 3.0198240e-06 *O 0.0000000e+00 0.0000000e+00 0.0000000e+00 1.3223646e-32 1.6572230e-24 4.9208050e-19 3.7800004e-15 2.8836393e-12 4.8262185e-10 2.8377452e-08 7.7581284e-07 1.1566324e-05 1.0177469e-04 5.3118366e-04 1.5427398e-03 2.6396911e-03 3.2768974e-03 3.4413927e-03 3.2942136e-03 2.9771550e-03 2.5861369e-03 2.1806906e-03 *OH 0.0000000e+00 0.0000000e+00 4.2291498e-26 8.0871899e-19 3.3433984e-14 4.3857807e-11 7.1379146e-09 3.1054585e-07 5.6770787e-06 5.7038009e-05 3.6939736e-04 1.6873015e-03 5.6759925e-03 1.4065446e-02 2.4754648e-02 3.2128829e-02 3.4775464e-02 3.4292083e-02 3.2135291e-02 2.9184849e-02 2.5940843e-02 2.2687255e-02 *O2 0.0000000e+00 0.0000000e+00 0.0000000e+00 6.7001339e-33 9.2977078e-25 3.2214171e-19 2.9689872e-15 2.7651925e-12 5.7354594e-10 4.2549860e-08 1.5057939e-06 3.0208651e-05 3.7897756e-04 3.0367852e-03 1.4223938e-02 3.8425545e-02 7.1551608e-02 1.0817919e-01 1.4519913e-01 1.8113182e-01 2.1532053e-01 2.4751071e-01 O3 0.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00 3.5360249e-32 7.1236640e-27 9.7023675e-23 2.0811585e-19 1.1726859e-16 2.3443091e-14 1.9511338e-12 6.8874057e-11 8.8099806e-10 4.1492288e-09 1.0184118e-08 1.7500625e-08 2.4579722e-08 3.0486964e-08 3.4803233e-08 3.7450542e-08 H2O(S) 3.0112050e-01 0.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00

More Answers (2)

Walter Roberson
Walter Roberson on 23 Oct 2023
If you want the transpose in the sense of having a row labeld Mix Ratio, and a row labeld '*H' and so on, then you have no hope of proceeding by way of printing all of the row names at one time and then going back and filling in the values.
If that is what you are after, I suggest you consider
T = array2table(fracs, 'VariableNames', pnames);
TT = rows2vars(T);
writetable(TT, 'ofmfT.txt', 'Delimiter', '\t', 'WriteVariableNames', false)
  12 Comments
Brantosaurus
Brantosaurus on 26 Oct 2023
I accepted your answer. Fabio's also worked with a little tweak.
I was just interested in a finnishing touch to Walter's slightly different approach.
Voss
Voss on 26 Oct 2023
I like Walter's approach too (that's why I voted for his answer).

Sign in to comment.


Fabio Freschi
Fabio Freschi on 23 Oct 2023
With mixed data tipes the simplest thing is to use a for loop
clear, clc, close all
%% input data
fracs = ...
[1.080799513888714E-32 0 0.62466759170135333 0.0742119108820972 0 0 0 ...
0 0 0.30112049741654717;
8.1862156579892249E-31 0 0.43700138755203366 0.56299861244797844 0 0 ...
0 0 0 0;
4.8637114770698571E-21 0 0.32440166506243995 0.67559833493756316 0 0 ...
4.2291497653345257E-26 0 0 0;
1.6450880329246443E-15 3.1789404533678006E-36 0.24933518340270872 0.7506648165972899 ...
3.0673853913761709E-28 1.3223645939372742E-32 8.0871899392903947E-19 ...
6.70013387010215E-33 0 0;
4.900395718623101E-12 6.1747782151010521E-28 0.19571626793147767 0.80428373206852888 ...
2.2269718209845535E-22 1.6572230040113729E-24 3.3433984090423735E-14 ...
9.2977078152200557E-25 0 0;
1.0257351304705146E-9 2.617799763018077E-22 0.15550208132805002 0.84449791867194646 ...
2.1781579716418966E-18 4.9208050395830782E-19 4.3857807128809733E-11 ...
3.2214171097127513E-19 0 0;
4.3103181910295979E-8 2.7279345388281687E-18 0.12422433753331349 0.87577561936350534 ...
1.570342966673278E-15 3.78000038794537E-15 7.1379146386309683E-9 2.9689871616039746E-15 ...
3.5360248709238178E-32 0;
6.5459411463843508E-7 2.7320582329382315E-15 0.099201583893637285 0.9007974509664014 ...
2.1726807488669134E-13 2.8836393026009774E-12 3.1054584734036675E-7 ...
2.7651925411612618E-12 7.12366402656099E-27 0;
5.0662163511599685E-6 5.883213268176446E-13 0.078724813502676808 0.92126444320230738 ...
1.0153013026281985E-11 4.8262185118338723E-10 5.6770786659506609E-6 ...
5.7354594102813562E-10 9.7023674645676979E-23 0;
2.4421990262674956E-5 4.403704051947919E-11 0.06164794655733434 0.93827052251646137 ...
2.2445716907411361E-10 2.8377451585746555E-8 5.7038008629661728E-5 4.25498603104255E-8 ...
2.0811584806073058E-19 0;
8.2187030635818051E-5 1.5288924079026876E-9 0.047173110175765094 0.95237302382510747 ...
2.8721600227496867E-9 7.7581284460813075E-7 0.00036939736170548689 1.5057939350703259E-6 ...
1.1726859150654507E-16 0;
0.00020427712746989916 2.9105403726253816E-8 0.034760508512691225 0.96330610874190448 ...
2.3876219545799965E-8 1.1566323656074547E-5 0.0016873015383648579 3.0208650517165084E-5 ...
2.3443091248967122E-14 0;
0.00038077633599091645 3.3138117176928766E-7 0.024151961677904805 0.96931004937101217 ...
1.3649589247496569E-7 0.00010177468680586972 0.0056759924892255493 0.00037897756200643907 ...
1.9511337609553526E-12 0;
0.00052407082677757269 2.2742164116491102E-6 0.015511740358074975 0.96632796330430382 ...
5.3611083709640121E-7 0.00053118366049772158 0.014065446275684821 0.0030367852474231624 ...
6.887405688319738E-11 0;
0.00052856823076589509 8.658464587260697E-6 0.0093989841734753513 0.9495411058272516 ...
1.3577058415442342E-6 0.0015427398356310203 0.024754647508174875 0.014223938254272516 ...
8.8099805664011839E-10 0;
0.00042813835721212761 1.8749212329738908E-5 0.0057655124991027457 0.920591265384587 ...
2.269548855159085E-6 0.0026396910954343466 0.032128828877358365 0.038425545025119787 ...
4.149228770119087E-9 0;
0.00031494793492961137 2.8327147863117975E-5 0.0037057710019004955 0.88634406267706345 ...
2.922132902838032E-6 0.0032768974022960775 0.034775463743648316 0.071551607959386754 ...
1.018411787725647E-8 0;
0.00022348304331810186 3.5251522913437423E-5 0.0024786422606670384 0.85134668127933666 ...
3.2746278191248033E-6 0.00344139271559275 0.034292082662405188 0.10817919188794742 ...
1.7500625275918122E-8 0;
0.00015605925600176512 3.9341075649633064E-5 0.001702599392503859 0.8174699691665277 ...
3.3970252161274673E-6 0.0032942135941524892 0.032135291011034732 0.14519912947891617 ...
2.4579722439036182E-8 0;
0.0001078783250984206 4.1056317609275734E-5 0.0011891055667362491 0.78536474093557762 ...
3.3613947843293898E-6 0.0029771549758717086 0.029184848667306983 0.18113182333004763 ...
3.0486964237300663E-8 0;
7.392252463130788E-5 4.0959000051015282E-5 0.00083862229703092787 0.755195725277905 ...
3.2225776517101929E-6 0.0025861369476422578 0.025940842802949327 0.2153205337688972 ...
3.4803232955347635E-8 0;
5.0210295664953338E-5 3.9555945293785451E-5 0.0005944585106563569 0.7269340589384562 ...
3.01982398381767E-6 0.0021806906182302303 0.022687255264208894 0.24751071315297202 ...
3.7450541969004565E-8 0];
of = [0.5; 1; 1.5; 2; 2.5; 3; 3.5; 4; 4.5; 5; 5.5; 6; 6.5; 7; 7.5; 8; 8.5; ...
9; 9.5; 10; 10.5; 11];
pnames = {'*H','HO2','*H2','H2O','H2O2','*O','*OH','*O2','O3','H2O(S)'};
%% print
pnums=length(pnames);
ofnums = length(of);
fileID = fopen('ofmfT.txt','w');
% data format
fmt = ['%15s ',repmat('%15.7e ', 1, ofnums), '%15.7e\n'];
% header
fprintf(fileID,'Mix ratio\n');
% loop
for i = 1:pnums
fprintf(fileID,fmt,pnames{i},fracs(:,i),of(i));
end
fclose(fileID);
  1 Comment
Stephen23
Stephen23 on 23 Oct 2023
Yes, a for loop is likely the simplest and clearest approach.
Without a loop is possible, but ... ugh, that cell array:
H = {'hello','world'};
M = randi(9,3,2);
Horizontal:
F = [repmat('%9d',1,2),'\n'];
fprintf('%9s',H{:})
hello world
fprintf('\n')
fprintf(F,M)
3 1 2 9 7 9
Vertical:
F = ['%9s',repmat('%9d',1,3),'\n'];
C = [H;num2cell(M)]; % ugh
fprintf(F,C{:})
hello 3 1 2 world 9 7 9

Sign in to comment.

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!