fprintf formatting problem with cell and array matrix 3
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
In relation to my previous post and accepted answer.
The outputs are just how i would like them.
Unfornunately the inputs are are actually the transpose of what they should be.
I slightly modified the code to try achieve the previous results, but it is not quire there. The data matrix is correct, but the fprintf output is not.
Any tweaks to correct this would be very much appreciated.
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
fileID = fopen('ofmf.txt','w');
fmt = ['%16s', repmat('%16.7e ', 1, numel(of)-1), '%16.7e\n'];
a = [{'mix-ratio'}, num2cell(of)];
b = [pnames, num2cell(fracs)];
data = [a; b];
fprintf(fileID,fmt,data{:});
fclose(fileID);
% check
type('ofmf.txt')
0 commentaires
Réponse acceptée
Voss
le 1 Nov 2023
Modifié(e) : Voss
le 1 Nov 2023
You just have to tranpose data as well:
data = [a; b].';
Full code:
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
fileID = fopen('ofmf.txt','w');
fmt = ['%16s', repmat('%16.7e ', 1, numel(of)-1), '%16.7e\n'];
a = [{'mix-ratio'}, num2cell(of)];
b = [pnames, num2cell(fracs)];
data = [a; b].';
fprintf(fileID,fmt,data{:});
fclose(fileID);
% check
type('ofmf.txt')
5 commentaires
Walter Roberson
le 1 Nov 2023
@Voss is correct. When you use fprintf(), all of the first parameter (after the format) will be "consumed" before any of the next parameter. This is a pain for constructing formatted output with multiple columns especially if multiple datatypes are involved.
%fprintf doesn't care about row vector vs column vector,
%it just uses all of the parameter before moving on to the next
fprintf('A<%g> B<%g>\n', [1 2], [3 4])
fprintf('A<%g> B<%g>\n', [1;2], [3;4])
%compose with column vectors processes a row at a time
compose('A<%g> B<%g>\n', [1;2], [3;4])
%I cannot explain this output for compose. The relevant doc says
%"If the number of columns in the last input array, AN, exceeds the
%number of remaining operators, then compose adds an additional
%column to str, as described in the previous syntax." -- but
%cannot understand at the moment why it repeats the previous output
compose('A<%g> B<%g> C<%g>\n', [1 2], [3 4])
%and what happens if we use row vectors with compose?
compose('A<%g> B<%g>\n', [1 2], [3 4])
%I think that's a bug compared to the documentation
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!