writing complex to a file
27 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
the cyclist
on 10 May 2014
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);
More Answers (1)
dpb
on 10 May 2014
>> 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.
0 Comments
See Also
Categories
Find more on Biological and Health Sciences in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!