MATLAB: Writing complex to a file

file

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

Best Answer

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);