fprintf formatting problem with cell and array matrix 2
이전 댓글 표시
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
Brantosaurus
2023년 10월 23일
Stephen23
2023년 10월 23일
It is not just a trivial transpose. Here are some approaches:
- use a loop and indexing,
- use a table (recommended),
- use e.g. COMPOSE and display the text,
- 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.
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')
fprintf('C')
fprintf('\n\n\n\n')
fprintf('D')
disp(string(newline)+newline+newline+newline)
fprintf('E')
It is a nuisance sometimes.
채택된 답변
추가 답변 (2개)
Walter Roberson
2023년 10월 23일
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
Star Strider
2023년 10월 23일
Brantosaurus is using R2017a. I don’t believe Brantosaurus can use ‘pnames’ as variable names in a table, since most of them aren’t valid MATLAB variable names. That ability arrived a couple releases later. if I remember correctly
Walter Roberson
2023년 10월 23일
Oh, good point about the version -- I didn't pay attention to that. rows2vars() was not introduced until R2018a, which IIRC was also the release that started supporting other variable names for tables.
Star Strider
2023년 10월 23일
My first thought initially was to use table until I checked that. The ‘pnames’ array won’t work as variable names.
Walter Roberson
2023년 10월 23일
Should still be able to do
T = [table(pnames(:), 'VariableNames', 'ID'), array2table(fracs.')];
writetable(T, 'ofmfT.txt', 'Delimiter', '\t', 'WriteVariableNames', false)
I just went back to R2016a to confirm (didn't have R2017a handy): Delimiter and WriteVariableNames were supported back then.
Star Strider
2023년 10월 23일
I didn’t check all the details. Avoiding writing the variable names would solve that problem.
Brantosaurus
2023년 10월 26일
Walter Roberson
2023년 10월 26일
T = [table(pnames(:), 'VariableNames', {'ID'}), array2table(fracs.')];
Brantosaurus
2023년 10월 26일
Voss
2023년 10월 26일
@Brantosaurus: See my complete solution, posted 3 days ago:
Walter Roberson
2023년 10월 26일
part1 = table([{'Mix ratio'}; pnames(:)], 'VariableNames', {'ID'});
part2 = array2table([of; fracs.']);
T = [part1, part2];
Brantosaurus
2023년 10월 26일
Voss
2023년 10월 26일
I like Walter's approach too (that's why I voted for his answer).
Fabio Freschi
2023년 10월 23일
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
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{:})
fprintf('\n')
fprintf(F,M)
Vertical:
F = ['%9s',repmat('%9d',1,3),'\n'];
C = [H;num2cell(M)]; % ugh
fprintf(F,C{:})
카테고리
도움말 센터 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!