Filter löschen
Filter löschen

How do I print complex number a+ib upto 6 decimal place in a loop.

3 Ansichten (letzte 30 Tage)
I have a complex (double) matrix st_matrix containing numbers in the form of a+ib. Now I want to print the values in a text file.
for p=1:101; % 101 is the number of rows.
s= st_matrix(p,:);
count=fprintf(fid ,'%.12f%+.10fli' , real(s), imag(s));
count=fprintf(fid,'\n');
end
My Expected output should be like :
1.96034545799687e-05 + 0.000756105063919229i -8.32404312346182e-05 + 0.000565437183372445i -1.54903072995014e-05 + 0.000410916702926380i
1.95824745829973e-05 + 0.000756115973057457i -8.44930558128954e-05 + 0.000564346616515616i -1.66587028148237e-05 + 0.000410464815615681i
1.95614932804376e-05 + 0.000756126810264354i -8.57391651447123e-05 + 0.000563240328264741i -1.78231150975604e-05 + 0.000409974438469278i
1.95405106806012e-05 + 0.000756137575538325i -8.69787095451668e-05 + 0.000562118367246730i -1.89831071531749e-05 + 0.000409445723268375i
1.95195267918012e-05 + 0.000756148268877799i -8.82116401021374e-05 + 0.000560980783378047i -2.01382434159039e-05 + 0.000408878835146654i
But the above code gives output as :
0.000019603455-0.0000832404j -0.000015490307+0.0000488314j 0.000022704867+0.0000323502j
0.000019582475-0.0000844931j -0.000016658703+0.0000491398j 0.000022313754+0.0000310167j
0.000019561493-0.0000857392j -0.000017823115+0.0000494415j 0.000021904664+0.0000296515j
0.000019540511-0.0000869787j -0.000018983107+0.0000497374j 0.000021478198+0.0000282570j
You can see that the real part of next element becomes of the complex part of the previous element. Any help would be appreciated.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 22 Mär. 2018
count=fprintf(fid ,'%.12f%+.10fli' , [real(s), imag(s)].'); %transpose is important
  2 Kommentare
sangeet sagar
sangeet sagar am 22 Mär. 2018
Sorry sir, but still same issue persists. I got the following out after editing from your code
0.000019603455-0.0000832404j -0.000015490307+0.0000488314j 0.000022704867+0.0000323502j
0.000019582475-0.0000844931j -0.000016658703+0.0000491398j 0.000022313754+0.0000310167j
0.000019561493-0.0000857392j -0.000017823115+0.0000494415j 0.000021904664+0.0000296515j
0.000019540511-0.0000869787j -0.000018983107+0.0000497374j 0.000021478198+0.0000282570j
Please observe that real part of (1,2) index becomex the complex part of (1,1) index.
Walter Roberson
Walter Roberson am 22 Mär. 2018
fprintf(fid ,'%.12f%+.10fj ' , [real(s); imag(s)]);
fprintf(fid, '\n');

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by