fprintf formatting problem with cell and array matrix

4 Ansichten (letzte 30 Tage)
Brantosaurus
Brantosaurus am 19 Okt. 2023
Kommentiert: Brantosaurus am 19 Okt. 2023
I would very much appreciate if somone could kindly show me how to finnish off what appears to be a simple formatting problem. Nearly there, but the last step has beaten me!
'ref' is what i want and 'ofmf' is the 'script' output.
Just a question of placing the 'mix ratio' character string immediately above the first data column and pushing the cell array one column to the right above the matrix. The number of columns and rows are unknown.
(Apologies for the messy data input!)
  1 Kommentar
Walter Roberson
Walter Roberson am 19 Okt. 2023
dbtype script
1 clear, clc, close all 2 %% input data 3 4 frac2d = ... 5 [1.080799513888714E-32 0 0.62466759170135333 0.0742119108820972 0 0 0 ... 6 0 0 0.30112049741654717; 7 8.1862156579892249E-31 0 0.43700138755203366 0.56299861244797844 0 0 ... 8 0 0 0 0; 9 4.8637114770698571E-21 0 0.32440166506243995 0.67559833493756316 0 0 ... 10 4.2291497653345257E-26 0 0 0; 11 1.6450880329246443E-15 3.1789404533678006E-36 0.24933518340270872 0.7506648165972899 ... 12 3.0673853913761709E-28 1.3223645939372742E-32 8.0871899392903947E-19 ... 13 6.70013387010215E-33 0 0; 14 4.900395718623101E-12 6.1747782151010521E-28 0.19571626793147767 0.80428373206852888 ... 15 2.2269718209845535E-22 1.6572230040113729E-24 3.3433984090423735E-14 ... 16 9.2977078152200557E-25 0 0; 17 1.0257351304705146E-9 2.617799763018077E-22 0.15550208132805002 0.84449791867194646 ... 18 2.1781579716418966E-18 4.9208050395830782E-19 4.3857807128809733E-11 ... 19 3.2214171097127513E-19 0 0; 20 4.3103181910295979E-8 2.7279345388281687E-18 0.12422433753331349 0.87577561936350534 ... 21 1.570342966673278E-15 3.78000038794537E-15 7.1379146386309683E-9 2.9689871616039746E-15 ... 22 3.5360248709238178E-32 0; 23 6.5459411463843508E-7 2.7320582329382315E-15 0.099201583893637285 0.9007974509664014 ... 24 2.1726807488669134E-13 2.8836393026009774E-12 3.1054584734036675E-7 ... 25 2.7651925411612618E-12 7.12366402656099E-27 0; 26 5.0662163511599685E-6 5.883213268176446E-13 0.078724813502676808 0.92126444320230738 ... 27 1.0153013026281985E-11 4.8262185118338723E-10 5.6770786659506609E-6 ... 28 5.7354594102813562E-10 9.7023674645676979E-23 0; 29 2.4421990262674956E-5 4.403704051947919E-11 0.06164794655733434 0.93827052251646137 ... 30 2.2445716907411361E-10 2.8377451585746555E-8 5.7038008629661728E-5 4.25498603104255E-8 ... 31 2.0811584806073058E-19 0; 32 8.2187030635818051E-5 1.5288924079026876E-9 0.047173110175765094 0.95237302382510747 ... 33 2.8721600227496867E-9 7.7581284460813075E-7 0.00036939736170548689 1.5057939350703259E-6 ... 34 1.1726859150654507E-16 0; 35 0.00020427712746989916 2.9105403726253816E-8 0.034760508512691225 0.96330610874190448 ... 36 2.3876219545799965E-8 1.1566323656074547E-5 0.0016873015383648579 3.0208650517165084E-5 ... 37 2.3443091248967122E-14 0; 38 0.00038077633599091645 3.3138117176928766E-7 0.024151961677904805 0.96931004937101217 ... 39 1.3649589247496569E-7 0.00010177468680586972 0.0056759924892255493 0.00037897756200643907 ... 40 1.9511337609553526E-12 0; 41 0.00052407082677757269 2.2742164116491102E-6 0.015511740358074975 0.96632796330430382 ... 42 5.3611083709640121E-7 0.00053118366049772158 0.014065446275684821 0.0030367852474231624 ... 43 6.887405688319738E-11 0; 44 0.00052856823076589509 8.658464587260697E-6 0.0093989841734753513 0.9495411058272516 ... 45 1.3577058415442342E-6 0.0015427398356310203 0.024754647508174875 0.014223938254272516 ... 46 8.8099805664011839E-10 0; 47 0.00042813835721212761 1.8749212329738908E-5 0.0057655124991027457 0.920591265384587 ... 48 2.269548855159085E-6 0.0026396910954343466 0.032128828877358365 0.038425545025119787 ... 49 4.149228770119087E-9 0; 50 0.00031494793492961137 2.8327147863117975E-5 0.0037057710019004955 0.88634406267706345 ... 51 2.922132902838032E-6 0.0032768974022960775 0.034775463743648316 0.071551607959386754 ... 52 1.018411787725647E-8 0; 53 0.00022348304331810186 3.5251522913437423E-5 0.0024786422606670384 0.85134668127933666 ... 54 3.2746278191248033E-6 0.00344139271559275 0.034292082662405188 0.10817919188794742 ... 55 1.7500625275918122E-8 0; 56 0.00015605925600176512 3.9341075649633064E-5 0.001702599392503859 0.8174699691665277 ... 57 3.3970252161274673E-6 0.0032942135941524892 0.032135291011034732 0.14519912947891617 ... 58 2.4579722439036182E-8 0; 59 0.0001078783250984206 4.1056317609275734E-5 0.0011891055667362491 0.78536474093557762 ... 60 3.3613947843293898E-6 0.0029771549758717086 0.029184848667306983 0.18113182333004763 ... 61 3.0486964237300663E-8 0; 62 7.392252463130788E-5 4.0959000051015282E-5 0.00083862229703092787 0.755195725277905 ... 63 3.2225776517101929E-6 0.0025861369476422578 0.025940842802949327 0.2153205337688972 ... 64 3.4803232955347635E-8 0; 65 5.0210295664953338E-5 3.9555945293785451E-5 0.0005944585106563569 0.7269340589384562 ... 66 3.01982398381767E-6 0.0021806906182302303 0.022687255264208894 0.24751071315297202 ... 67 3.7450541969004565E-8 0]; 68 69 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; ... 70 9; 9.5; 10; 10.5; 11]; 71 72 prodNames = cell(1, 10); 73 prodNames{1} = '*H'; 74 prodNames{2} = 'HO2'; 75 prodNames{3} = '*H2'; 76 prodNames{4} = 'H2O'; 77 prodNames{5} = 'H2O2'; 78 prodNames{6} = '*O'; 79 prodNames{7} = '*OH'; 80 prodNames{8} = '*O2'; 81 prodNames{9} = 'O3'; 82 prodNames{10} = 'H2O(S)'; 83 84 %% print 85 86 prodNums = length(prodNames); 87 88 fileID = fopen('ofmf.txt','w'); 89 fprintf(fileID,' Mix ratio\n'); 90 fmt1 = [repmat('%15s ', 1, prodNums-1), '%15s\n']; 91 fprintf(fileID,fmt1,prodNames{:}); 92 fmt2 = [repmat('%15.7e ', 1, prodNums), '%15.7e\n']; 93 fprintf(fileID,fmt2,[of,frac2d].'); 94 fprintf(fileID,' **************\n'); 95 96 fclose('all');

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Star Strider
Star Strider am 19 Okt. 2023
Eliminiating the newline character from the ‘Mix ratio’ fprintf statement:
fprintf(fileID,' Mix ratio');
produces:
Mix ratio *H HO2 *H2 H2O H2O2 *O *OH *O2 O3 H2O(S)
5.0000000e-01 1.0807995e-32 0.0000000e+00 6.2466759e-01 7.4211911e-02 0.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00 3.0112050e-01
1.0000000e+00 8.1862157e-31 0.0000000e+00 4.3700139e-01 5.6299861e-01 0.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00
1.5000000e+00 4.8637115e-21 0.0000000e+00 3.2440167e-01 6.7559833e-01 0.0000000e+00 0.0000000e+00 4.2291498e-26 0.0000000e+00 0.0000000e+00 0.0000000e+00
2.0000000e+00 1.6450880e-15 3.1789405e-36 2.4933518e-01 7.5066482e-01 3.0673854e-28 1.3223646e-32 8.0871899e-19 6.7001339e-33 0.0000000e+00 0.0000000e+00
2.5000000e+00 4.9003957e-12 6.1747782e-28 1.9571627e-01 8.0428373e-01 2.2269718e-22 1.6572230e-24 3.3433984e-14 9.2977078e-25 0.0000000e+00 0.0000000e+00
3.0000000e+00 1.0257351e-09 2.6177998e-22 1.5550208e-01 8.4449792e-01 2.1781580e-18 4.9208050e-19 4.3857807e-11 3.2214171e-19 0.0000000e+00 0.0000000e+00
3.5000000e+00 4.3103182e-08 2.7279345e-18 1.2422434e-01 8.7577562e-01 1.5703430e-15 3.7800004e-15 7.1379146e-09 2.9689872e-15 3.5360249e-32 0.0000000e+00
4.0000000e+00 6.5459411e-07 2.7320582e-15 9.9201584e-02 9.0079745e-01 2.1726807e-13 2.8836393e-12 3.1054585e-07 2.7651925e-12 7.1236640e-27 0.0000000e+00
4.5000000e+00 5.0662164e-06 5.8832133e-13 7.8724814e-02 9.2126444e-01 1.0153013e-11 4.8262185e-10 5.6770787e-06 5.7354594e-10 9.7023675e-23 0.0000000e+00
5.0000000e+00 2.4421990e-05 4.4037041e-11 6.1647947e-02 9.3827052e-01 2.2445717e-10 2.8377452e-08 5.7038009e-05 4.2549860e-08 2.0811585e-19 0.0000000e+00
5.5000000e+00 8.2187031e-05 1.5288924e-09 4.7173110e-02 9.5237302e-01 2.8721600e-09 7.7581284e-07 3.6939736e-04 1.5057939e-06 1.1726859e-16 0.0000000e+00
6.0000000e+00 2.0427713e-04 2.9105404e-08 3.4760509e-02 9.6330611e-01 2.3876220e-08 1.1566324e-05 1.6873015e-03 3.0208651e-05 2.3443091e-14 0.0000000e+00
6.5000000e+00 3.8077634e-04 3.3138117e-07 2.4151962e-02 9.6931005e-01 1.3649589e-07 1.0177469e-04 5.6759925e-03 3.7897756e-04 1.9511338e-12 0.0000000e+00
7.0000000e+00 5.2407083e-04 2.2742164e-06 1.5511740e-02 9.6632796e-01 5.3611084e-07 5.3118366e-04 1.4065446e-02 3.0367852e-03 6.8874057e-11 0.0000000e+00
7.5000000e+00 5.2856823e-04 8.6584646e-06 9.3989842e-03 9.4954111e-01 1.3577058e-06 1.5427398e-03 2.4754648e-02 1.4223938e-02 8.8099806e-10 0.0000000e+00
8.0000000e+00 4.2813836e-04 1.8749212e-05 5.7655125e-03 9.2059127e-01 2.2695489e-06 2.6396911e-03 3.2128829e-02 3.8425545e-02 4.1492288e-09 0.0000000e+00
8.5000000e+00 3.1494793e-04 2.8327148e-05 3.7057710e-03 8.8634406e-01 2.9221329e-06 3.2768974e-03 3.4775464e-02 7.1551608e-02 1.0184118e-08 0.0000000e+00
9.0000000e+00 2.2348304e-04 3.5251523e-05 2.4786423e-03 8.5134668e-01 3.2746278e-06 3.4413927e-03 3.4292083e-02 1.0817919e-01 1.7500625e-08 0.0000000e+00
9.5000000e+00 1.5605926e-04 3.9341076e-05 1.7025994e-03 8.1746997e-01 3.3970252e-06 3.2942136e-03 3.2135291e-02 1.4519913e-01 2.4579722e-08 0.0000000e+00
1.0000000e+01 1.0787833e-04 4.1056318e-05 1.1891056e-03 7.8536474e-01 3.3613948e-06 2.9771550e-03 2.9184849e-02 1.8113182e-01 3.0486964e-08 0.0000000e+00
1.0500000e+01 7.3922525e-05 4.0959000e-05 8.3862230e-04 7.5519573e-01 3.2225777e-06 2.5861369e-03 2.5940843e-02 2.1532053e-01 3.4803233e-08 0.0000000e+00
1.1000000e+01 5.0210296e-05 3.9555945e-05 5.9445851e-04 7.2693406e-01 3.0198240e-06 2.1806906e-03 2.2687255e-02 2.4751071e-01 3.7450542e-08 0.0000000e+00
**************
Is that what you want?
Writing it to a table and then using writetable would be easier, however that would not work in R2017a, because only valid MATLAB variable names were allowed as variable names then. The ability to use variable names such as you want was introduced in R2020b (if I remember correctly).
.
  1 Kommentar
Brantosaurus
Brantosaurus am 19 Okt. 2023
Jeepers, i missed that!!!
Just had a hunch it was possible with my outdated version.
This is the easiest solution to my problem and take note of your advice.
The other contributions are also much appreciated and will undoubtedly widen my understanding by different approaches. Thanks guys :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Walter Roberson
Walter Roberson am 19 Okt. 2023
Verschoben: Walter Roberson am 19 Okt. 2023
I would suggest that you use compose as it makes it easier to create rows of data. If you use a "" quoted string for the format instead of a '' quoted string then the output of compose() would be a string() array that you could then strjoin with newline and fwrite the result.

Voss
Voss am 19 Okt. 2023
See the attached modified script and resulting .txt file.
The file-writing section is reproduced here:
prodNums = length(prodNames);
fileID = fopen('ofmf.txt','w');
fmt1 = [repmat('%15s ', 1, prodNums), '%15s\n'];
temp = [{'Mix ratio'} prodNames];
fprintf(fileID,fmt1,temp{:});
fmt2 = [repmat('%15.7e ', 1, prodNums), '%15.7e\n'];
fprintf(fileID,fmt2,[of,frac2d].');
fprintf(fileID,' **************\n');
fclose(fileID);

Kategorien

Mehr zu Data Types finden Sie in Help Center und File Exchange

Produkte


Version

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by