How to include complex numbers in fprintf function?

Technical Source
1 min readDec 5, 2023

--

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.

NOTE:-

Matlabsolutions.com provide latest MatLab Homework Help,MatLab Assignment Help , Finance Assignment Help for students, engineers and researchers in Multiple Branches like ECE, EEE, CSE, Mechanical, Civil with 100% output.Matlab Code for B.E, B.Tech,M.E,M.Tech, Ph.D. Scholars with 100% privacy guaranteed. Get MATLAB projects with source code for your learning and research.

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

SEE COMPLETE ANSWER CLICK THE LINK

--

--

Technical Source
Technical Source

Written by Technical Source

Simple! That is me, a simple person. I am passionate about knowledge and reading. That’s why I have decided to write and share a bit of my life and thoughts to.

No responses yet