How to include complex numbers in fprintf function?

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.

 Respuesta aceptada

Star Strider
Star Strider el 28 de Nov. de 2017
Editada: Star Strider el 28 de Nov. de 2017
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 comentarios

Thanks for the quick response! Can you see where I need to enter something similar to your example in my code as I am only needing to produce the complex numbers for V? When I enter something similar to your example I'm still not receiving complex numbers.
My pleasure.
I cannot run your code. I have no idea what ‘V’ is, or what ‘block_of_data’ is.
If ‘V’ is a vector, this works:
fid = 1;
x = sqrt(randi([-5 5], 5, 1));
fprintf(fid, '%f%+fj\n', [real(x), imag(x)].')
In all likelihood, if ‘V’ is complex, ‘block_of_data’ will also be complex (although the imaginary parts of the real numbers will be 0), considerably complicating your desire to use otherwise efficient matrix arguments to fprintf.
A for loop is likely your best option:
fid = 1;
probe_r = randi(9, 5, 1);
probe_theta = randi(9, 5, 1)
V = sqrt(randi([-5 5], 5, 1));
for k1 = 1:size(probe_r,1)
fprintf(fid, '%.1f %.1f %f%+fj\n', probe_r(k1), probe_theta(k1), real(V(k1)), imag(V(k1)))
end
It is not inefficient if it is the only way to do what you want.
Thanks a lot for your answer, much appreciated. Unfortunately, I think I'm a little out my depth. I am getting closer but instead of showing the complex number that V gives the code produces the complex number as the next real number in the array. I have no doubt its me thats making the mistakes and not yourself! Apologies for taking up your time.
column_names = {'r', 'theta', 'V'};
fid = fopen('fidtext.txt','wt');
block_of_data = [probe_r, probe_theta]
fmt = repmat('%15g ', 1, 3);
fmt(end:end+1) = '\n';
fprintf(fid, '%f%+fi\n', [real(V), imag(V)].');
fprintf(fid, fmt, block_of_data.');
fclose(fid);
When I simulated ‘block_of_data’ as two columns of random real numbers and a complex vector ‘V’, ‘block_of_data’ became a complex matrix. That is the reason I used the for loop with the individual columns of the matrix.
If you separate out the real and imaginary parts of ‘V’, this works:
block_of_data = [probe_r probe_theta real(V) imag(V)];
fprintf(fid, '%.1f %.1f %.1f%+.1fj\n', block_of_data')
Change the format string to the one you want.
Try
fprintf(fid, '%f%+fi\n', [real(V(:)), imag(V(:))].');
You've only gone and done it!
Thank you both for your help, it is hugely appreciated.
Our pleasure!
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.

Iniciar sesión para comentar.

Más respuestas (1)

Ken
Ken el 6 de Feb. de 2020
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 comentarios

Ken
Ken el 6 de Feb. de 2020
But I am using your code example and getting the wrong result. Did the Matlab version change this? Please run my snippet on your machine and view text file. Thanks.
@Ken Crandall —
But I am using your code example ...
No, you’re not. Note the concatenation of the two vectors in a matrix, and the transpose operator (.') in Walter’s code, however not in yours.
The concatenation and transpose are very important for this purpose .
Ken Crandall’s Answer moved here —
I apologize. Thanks!
No apology necessary!
It’s just that it’s important to read the code carefully and understand how it works.
Thank you. It worked.

Iniciar sesión para comentar.

Categorías

Más información sobre Entering Commands en Centro de ayuda y File Exchange.

Preguntada:

el 28 de Nov. de 2017

Comentada:

el 24 de En. de 2025

Community Treasure Hunt

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

Start Hunting!

Translated by