MATLAB: Displaying matrix in .txt file (fprintf)

aligenddisplayingfprintffprintf_aligendhelpMATLABmatrixproblem

Hello.
I have matrix with complex numbers with different numbers of digits. I want to print them to the file but they are not aligned.
|1.00+0.00i 1.00+0.00i 1.00+0.00i 1.00+0.00i|
|0.00+0.00i -49.00+4.00i -49.00-4.00i -52.00+0.00i|
|0.00+0.00i -2417.00-0.00i -2417.00+0.00i -2392.00+0.00i|
|0.00+0.00i -117649.00-64.00i -117649.00+64.00i -117676.00+0.00i|
I need something like this:
|1.00+0.00i 1.00+0.00i 1.00+0.00i 1.00+0.00i|
|0.00+0.00i -49.00+4.00i -49.00-4.00i -52.00+0.00i|
|0.00+0.00i -2417.00-0.00i -2417.00+0.00i -2392.00+0.00i|
|0.00+0.00i -117649.00-64.00i -117649.00+64.00i -117676.00+0.00i|
This is code that I print it into file
for k=1:n
fprintf(fileID,' |%.2f%+.2fi %.2f%+.2fi %.2f%+.2fi %.2f%+.2fi|\r\n',[real(A(k,:));imag(A(k,:))]);
end
fprintf(fileID, '\r\n\r\n\r\n');
Is there any way to make this matrix more aligned?

Best Answer

Just tweaked you code itself a little bit:
fileID=fopen('MyFile.txt','w'); % Open text file for writing
for k=1:3
fprintf(fileID,' |%.2f%+.2fi %10.2f%+.2fi %10.2f%+.2fi %10.2f%+.2fi|\r\n',[real(A(k,:));imag(A(k,:))]);
end
fprintf(fileID,' |%.2f%+.2fi %10.2f%+.2fi %10.2f%+.2fi %10.2f%+.2fi|\r\n',[real(A(4,:));imag(A(4,:))]);
fprintf(fileID, '\r\n\r\n\r\n');
fclose(fileID); % Close the file
Gives:
Capture.JPG