Using fprintf for complex numbers
Ältere Kommentare anzeigen
Hi, I have
Z1= sqrt(3) * exp((pi*i)/4)
Z2= 2 * exp((pi*i)/6)
Z3= sqrt(3) * exp((3*pi*i)/4)
Z4= 2 * exp(pi*i)
Z5= 2 * exp(-pi*i)
And I would like something like
Real Complex
Z1 1.2247 + 1.2247i
Z2 1.7321 + 1.0000i
Z3 -1.2247 + 1.2247i
Z4 -2.0000 + 0.0000i
Z5 -2.0000 - 0.0000i
Using fprintf, I would think...
Thanks for your help!
Akzeptierte Antwort
Weitere Antworten (4)
a = -2*sqrt(-3)+5/3
num2str(a)
num2str(a, 2) % specify precision
You would enter this as a string in fprintf
ijstr = num2str(a,3);
fprintf('%s %s', 'Z1', ijstr)
Note that this method may return strings in undesired formats such as,
num2str(160, 2)
5 Kommentare
marcel hendrix
am 26 Nov. 2024
p = [ 9.542130347090435e-01 + 1.371703909876786e-01i
9.542130347090435e-01 - 1.371703909876786e-01i ];
fprintf(1,'%s ',num2str(p,10));
>> 00..99554422113300334477+-00..113377117700339911ii
What went wrong here?
Les Beckham
am 26 Nov. 2024
Bearbeitet: Les Beckham
am 26 Nov. 2024
I'm not sure I can explain exactly why it does that, other than it appears to be pulling the characters out of the output of num2str by going down the columns of the 2x25 char array.
p = [ 9.542130347090435e-01 + 1.371703909876786e-01i
9.542130347090435e-01 - 1.371703909876786e-01i ];
num2str(p, 10)
fprintf('%s ', num2str(p,10)); % << note that you don't need the "1," stdout (1) is the default
One way to get what you probably want is this
c = compose('%s', num2str(p,10))
fprintf('%s\n', c{:})
Walter Roberson
am 26 Nov. 2024
Bearbeitet: Walter Roberson
am 26 Nov. 2024
it appears to be pulling the characters out of the output of num2str by going down the columns of the 2x25 char array.
That is exactly what it is doing. fprintf() always follows down the columns of the first parameter, completely using up the first parameter before going on to follow down the columns of the second parameter, and so on. To put it another way: fprintf() uses linear indexing order of each parameter.
p = [ 9.542130347090435e-01 + 1.371703909876786e-01i
9.542130347090435e-01 - 1.371703909876786e-01i ];
fprintf('%s ', num2str(p,10).');
Note: careful examination will show that the blank from the format is at the end of the output only. This is due to the way that char() arrays are processed.
marcel hendrix
am 30 Nov. 2024
Thanks, I was on the verge of submitting a bug report...
Processing matrix arguments by column has another surprise: if one prints a matrix to a file for subsequent processing in another program, the matrix will look transposed to the post-processor. I can say from experience that this bug (in the post-processor and caused by the reverse side of the keyboard) can be very hard to find ...
Note that you can avoid the c{:} step if you pass compose a string scalar format instead of a character vector format.
p = [ 9.542130347090435e-01 + 1.371703909876786e-01i
9.542130347090435e-01 - 1.371703909876786e-01i ];
c = compose("%s", num2str(p,10))
fprintf('%s\n', c)
And that leads to the more compact
fprintf('%s\n', compose("%s", num2str(p,10)))
Walter Roberson
am 27 Aug. 2019
Bearbeitet: Walter Roberson
am 27 Aug. 2019
fprintf() always ignores imaginary components of numbers. You need to ask to output the real() and imaginary() components separately,
fprintf('%2s %7.4f %+7.4fi\n', 'Z1', real(Z1), imag(Z1))
x = [
2.34 + 4.35i
6.34 + 4.32i
7.45 + 3.21i
8.45 + 9.23i
];
fprintf('%0.2f + i%0.2f\r', [real(x) imag(x)].');
You may want to use + in the format specifier.
A = [3+4i, 5, 6-7i]
fprintf("%d%+di\t", [real(A); imag(A)])
Kategorien
Mehr zu Whos finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
