How to include complex numbers in fprintf function?

33 ビュー (過去 30 日間)
Robbie McCormack
Robbie McCormack 2017 年 11 月 28 日
コメント済み: Walter Roberson 2025 年 1 月 24 日
lambda = [1.064e-6];
R = [30];
w=[0.001];
q = (1./R- i* lambda./pi./w.^2).^(-1);
a=1;
p=1;
m=1;
probe_r=linspace(0,0.003,100);
probe_theta=linspace(0,0.003,100);
rseed=[0*max(w):max(w)/30:3*max(w)];
thetaseed=[0:360]*pi/180;
[r,theta]=meshgrid(rseed,thetaseed);
E=LaguerreGaussianE([p,m,q,lambda,a],r,theta);
V=interp2(r,theta,E,probe_r,probe_theta);
column_names = {'r', 'theta', 'V'};
fid = fopen('fidtext.txt','wt');
fprintf(fid, '%s ', column_names{:});
fprintf(fid, '\n');
block_of_data = [probe_r, probe_theta, V];
fmt = repmat('%15g ', 1, 3);
fmt(end:end+1) = '\n';
fprintf(fid, fmt, block_of_data.');
fclose(fid);
With the current code I have I get a .txt file of only the real numbers from my function V along with the values of probe_r and probe_theta. How do I alter this to produce both the real and complex numbers as a 3 column .txt file of r, theta and V as I am unable to see a formatSpec to include complex numbers.

採用された回答

Star Strider
Star Strider 2017 年 11 月 28 日
編集済み: Star Strider 2017 年 11 月 28 日
you have to write the real and complex parts separately.
Example
x = sqrt(-2);
fprintf(fid, '%f%+fj\n', real(x), imag(x))
0.000000+1.414214j
  8 件のコメント
Star Strider
Star Strider 2017 年 11 月 28 日
Our pleasure!
Walter Roberson
Walter Roberson 2025 年 1 月 24 日
V = [-1 - 1i
-1 + 0i
-1 + 1i
0 - 1i
0 - 0i
0 + 1i
1 - 1i
1 + 0i
1 + 1i];
fid = 1;
fprintf(fid, '%f%+fi\n', [real(V(:)), imag(V(:))].');
-1.000000-1.000000i -1.000000+0.000000i -1.000000+1.000000i 0.000000-1.000000i 0.000000+0.000000i 0.000000+1.000000i 1.000000-1.000000i 1.000000+0.000000i 1.000000+1.000000i
You can see that negative imaginary parts are automatically handled.
The key here is the %+ specification, which instructs that the appropriate sign of the value is to be inserted.

サインインしてコメントする。

その他の回答 (1 件)

Ken Crandall
Ken Crandall 2020 年 2 月 6 日
This works fine for a single complex number. If you replace x with an array, the reals come first followed by all the imaginaries.
For example:
rxSignal=[1.1+1j*2.2 3.3+1j*4.4 5.5+1j*6.6]
fileID=fopen('rxSignal.txt','w')
fprintf(fileID,'%f%+fj\n',real(rxSignal(:)),imag(rxSignal(:)));
fclose(fileID)
When you read the text file, you get:
1.100000+3.300000j
5.500000+2.200000j
4.400000+6.600000j
Here you see the reals filling the first three slots and the imaginaries filling the last three slots.
How to fix this without writing a for loop to do one at a time?
  7 件のコメント
Star Strider
Star Strider 2020 年 2 月 7 日
No apology necessary!
It’s just that it’s important to read the code carefully and understand how it works.
Paul Serna-Torre
Paul Serna-Torre 2023 年 12 月 4 日
Thank you. It worked.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by