MATLAB: Display array values side by side

MATLAB

I have two arrays 'real' and 'imag' and this needs to be copied to a text file side by side. I tried the below as per Displaying data side by side
real = 1:5;
imag = 6:10;
fileID = fopen('data.txt','w');
fprintf(fileID,'%8d\t %8d\n',[real, imag]');
fclose(fileID);
This doesn't seem to work as it displays:
1 2
3 4
5 6
7 8
9 10
Expected output:
1 6
2 7
3 8
4 9
5 10

Best Answer

Force ‘real’ and ‘imag’ to become column vectors first:
fprintf(fileID,'%8d\t %8d\n',[real(:), imag(:)]')
or vertically concatenate them:
fprintf(fileID,'%8d\t %8d\n',[real; imag])
Both will produce your desired result.