MATLAB Answers

How to save a complex matrix as text file?

17 views (last 30 days)
Nurulhuda Ismail
Nurulhuda Ismail on 4 Dec 2019
Edited: dpb on 5 Dec 2019
Hi, I would like to extract my complex matrix from MATLAB to text file. How am I going to do it?
I have tried to use the code below, however, what is appear in the text file is just the real part of the elements. How to display both real and imaginary part?
fid = fopen('z.txt','wt');
for ii = 1:size(Z,1)
fprintf(fid,'%g\t',Z(ii,:));
fprintf(fid,'\n');
end
fclose(fid)

  0 Comments

Sign in to comment.

Accepted Answer

dpb
dpb on 4 Dec 2019
Edited: dpb on 4 Dec 2019
C (and ergo Matlab) is woefully weak in input/output for complex variables. Should been remedied by TMW from the git-go given Matlab is a computational engine and complex variables are inherent in much of science/engineering. But, that aside, one "trick" you can use if you aren't too particular about the output format is
>> z=complex(rand(3),rand(3)) % create sample data array (too bad TMW didn't wrap the default output engine)
z =
0.1855 + 0.2663i 0.0469 + 0.9033i 0.9006 + 0.8023i
0.8028 + 0.1542i 0.0428 + 0.7362i 0.7809 + 0.5418i
0.8315 + 0.5255i 0.5429 + 0.9076i 0.8857 + 0.4982i
>>
then
writetable(table(z),'writevariablenames',0)
will give you
>> type table.txt
0.185524409358458+0.0469069782405116i,0.900606398311369+0.266286079870998i,0.903297636775399+0.802283416421606i
0.802798214229172+0.0428008632408867i,0.780920556942062+0.154178330544604i,0.736241861039116+0.541798940411929i
0.831494678986611+0.542863198858789i,0.8856901938773+0.525517402969329i,0.907584220125958+0.498240086285874i
>>
Alternatively, write the loop but explicit format for both real, imaginary parts...
fmt=[repmat('%g\t%gi\t',1,size(z,2)-1) '%g\t%gi\n'];
for i=1:size(z,1)
fprintf(fmt,reshape(reshape([real(z(i,:)),imag(z(i,:))],size(z,2),[]).',1,[]))
end
Alternatively, can simplify the organization expression significantly by just writing the explicit double loop...
Another place where a Fortran idiom is really useful...the implied DO so don't have to write the full explicit second for loop.
I remain amazed TMW has never seen fit to deal with such trivialities in 40+ years... :(

  2 Comments

Nurulhuda Ismail
Nurulhuda Ismail on 4 Dec 2019
I have tried to applied your suggestion, unfortunately it is not what we expect in text file:
clear all; close all; clc;
z =complex(rand(3),rand(3))
fmt=[repmat('%g\t%gi\t',1,size(z,2)-1) '%g\t%gi\n'];
for i=1:size(z,1)
fprintf(fmt,reshape(reshape([real(z(i,:)),imag(z(i,:))],size(z,2),[]).',1,[]))
end
fid = fopen('B.txt','wt');
for ii = 1:size(fmt,1)
fprintf(fid,'%g\t',fmt(ii,:));
fprintf(fid,'\n');
end
fclose(fid)
Suppose the result should be like this:
0.724849 0.0624742i 0.39391 0.63923i 0.226536 0.059973i
0.38886 0.578408i 0.923302 0.147502i 0.742268 0.0575426i
0.218462 0.990951i 0.346188 0.739458i 0.809823 0.911036i
But the result in B.txt:
37 103 92 116 37 103 105 92 116 37 103 92 116 37 103 105 92 116 37 103 92 116 37 103 105 92 110
Do you have any idea to fix it? Thank you.
dpb
dpb on 4 Dec 2019
Yeah, use what I wrote except direct it to the file instead of the screen as I did (by not using a file handle, that's the default action of fprintf)
Your line
fprintf(fid,'%g\t',fmt(ii,:));
writes the format string character representation as decimal, not the output array which can be seen by
>> char([37 103 92 116 37 103 105 92 116 37 103 92 116 37 103 105 92 116 37 103 92 116 37 103 105 92 110])
ans =
'%g\t%gi\t%g\t%gi\t%g\t%gi\n'
>>
fprintf(fid,fmt,reshape(reshape([real(z(i,:)),imag(z(i,:))],size(z,2),[]).',1,[]))
is the line in the loop; z is the complex array to write.

Sign in to comment.

More Answers (0)

Sign in to answer this question.