How to save a complex matrix as text file?
35 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Nurulhuda Ismail
el 4 de Dic. de 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 comentarios
Respuesta aceptada
dpb
el 4 de Dic. de 2019
Editada: dpb
el 4 de Dic. de 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 comentarios
dpb
el 4 de Dic. de 2019
Editada: dpb
el 5 de Dic. de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Data Type Conversion en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!