0

I have an fprintf statement which loops 3 times in order to display some data. After the first iteration, MATLAB displays a mysterious space even though I have not added an extra \t. It acts as if I had an if statement to display a different fprintf statement after the first iteration, but I have nothing like that on the code. See picture on the link for the result it displays

% Display results

fprintf('Panel\tPressure  Cl\tCd\t| Panel\tPressure  Cl\tCd\n')
for q = 1:length(AOA)
       fprintf('--------------\t-------\t------- |--------------\t-- 
        -----\t-------\n')             
       fprintf('AOA %.0f°\t\t%.4f\t%.4f\t|AOA %.0f°
       \t\t%.4f\t%.4f\n'...
       ,AOA(q),Cl(q),CD(q),AOA(q),ClFinal(q),CDFinal(q))
       fprintf('--------------\t-------\t------- |--------------\t-- 
       -----\t-------\n')   
    for j = 1:length(pressure{1})
       fprintf('%.0f\t%.4f\t    |\t  |\t|%.0f\t%.4f\n',j+1,pressure{q} 
       (j),j+1,pFinal{q}(j))       
    end
end
medicine_man
  • 321
  • 3
  • 15

1 Answers1

0

When you fprintf a \t character, there is an automatic space padding up to 4 spaces. If the string has less than 4 characters, the string will be placed at the start and be "space padded" until 4 characters have been filled (in reality, the space padded characters resemble just one character). If the string has more than 4 characters, then it will space pad at 8, 12, 16, etc...

Here is what your question is really about:

fprintf('Panel\tPressure Cl\tCd\t| Panel\tPressure Cl\tCd\n')

The first string Panel has 5 characters, and therefore will be space padded with the equivalent of 3 spaces at the end of the first Panel. However, the second string | Panel has 7 characters, and therefore will only need the equivalent of 1 space at the end of the second string.

To remove your spacing issue, and have a more uniform spacing between your text headers, you can place a tab character after every header you want, and change your formating for your other fprintf statements accordingly:

fprintf('Panel\tPressure\tCl\t\tCd\t\t|\tPanel\tPressure\tCl\t\tCd\n')

You can also view this link for another example of how space padding works.

Also, here is the MATLAB Documentation on Formatting Text.

medicine_man
  • 321
  • 3
  • 15