Search code examples
matlabalignmentprintfspacing

MATLAB create constant spacing between numbers on same line


Good morning,

I'm sure there's a built in function but I can't find it. I want to create static positioning for information being sent to a text document in MATLAB. For example:

height          weight          age          favorite number
------------------------------------------------------------     
60              140             24           9
30              45              3            10000000
48              100             9            19
9               7               1            1

currently, i'm just doing an fprint call with padded spaces to get it lined up, but the issue arises where having different length numbers causes the alignment to be off, like so:

height          weight          age          favorite number
------------------------------------------------------------     
60              140             24           9
30              45             3           10000000
48              100             9           19
1              7              1           1

Thanks in advance.

here's an example script that'll show what I mean:

fid1 = fopen('stackoverflowtest', 'w');
if fid1 < 3,
  error('ERROR');
end;

fprintf(fid1, 'height          weight          age          favorite number \n');
fprintf(fid1, '------------------------------------------------------------ \n');

height = 0;
weight = 10;
age = 100;
number = 3;

for i = 1:100
    fprintf(fid1, "%d              ', height);
    fprintf(fid1, "%d              ', weight);
    fprintf(fid1, "%d          ', age);
    fprintf(fid1, "%d \n", number);

    height = height + 3;
    weight = weight + 6;
    age = age - 1;
    number = number + 23;
end

Solution

  • You can do this with the fprintf format specification, for example %-15d.

    Here, the - is a flag which specifies left justification, and the 15 specifies how much space to leave around the representation.

    We can reproduce your example with

    A = [60 140 24 9
         30 45  3  10000000
         48 100 9  19
         9  7   1  1];
    fprintf('height          weight          age          favorite number \n'),...
    fprintf('------------------------------------------------------------ \n'),...
    fprintf('%-15d %-15d %-12d %-15d \n',A')
    

    which displays

    height          weight          age          favorite number 
    ------------------------------------------------------------ 
    60              140             24           9               
    30              45              3            10000000        
    48              100             9            19              
    9               7               1            1   
    

    EDIT: You can store this data as a table:

    height = A(:,1)
    weight = A(:,2);
    age  = A(:,3);
    favourite_number = A(:,4);
    tab1 = table(height, weight, age, favourite_number);
    disp(tab1);
    

    This prints to screen

    height    weight    age    favourite_number
    ______    ______    ___    ________________
    
    60        140       24         9           
    30         45        3     1e+07           
    48        100        9        19           
     9          7        1         1       
    

    but I'm not sure how to save this representation to a file.