writing complex to a file

29 次查看(过去 30 天)
yasser
yasser 2014-5-10
评论: yasser 2014-5-10
i need to write a complex vector to a file from matlab
i tried
clear;
A=complex(randn(1,5)/sqrt(2),randn(1,5)/sqrt(2));
for k=1:length(A)
tmp=num2str(A(k));
fileID = fopen('tst.txt','wt');
fprintf(fileID,'%s\n',tmp);
fclose(fileID);
end
but it gives only single line in file

采纳的回答

the cyclist
the cyclist 2014-5-10
Only open and close the file once, not repeatedly in the loop:
clear;
A=complex(randn(1,5)/sqrt(2),randn(1,5)/sqrt(2));
fileID = fopen('tst.txt','wt');
for k=1:length(A)
tmp=num2str(A(k));
fprintf(fileID,'%s\n',tmp);
end
fclose(fileID);

更多回答(1 个)

dpb
dpb 2014-5-10
>> z
z =
2.7247 + 0.0875i 7.7582 + 0.3089i 3.3141 + 0.2309i 6.0307 + 0.9092i 1.8401 + 0.9369i
>> fmt=['%.2f + %.2fi \n'];
>> fprintf(fmt,[real(z); imag(z)])
2.72 + 0.09i
7.76 + 0.31i
3.31 + 0.23i
6.03 + 0.91i
1.84 + 0.94i
>>
Salt format to suit...above is for human consumption.

类别

Help CenterFile Exchange 中查找有关 Biological and Health Sciences 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by