print weight data and height data in two aligned columns in matlab

You can use the fprintf() function in MATLAB to print weight and height data in two aligned columns. Here is an example code snippet:

main.m
weights = [72.5, 68.3, 81.2, 64.7, 76.9];
heights = [1.75, 1.62, 1.83, 1.54, 1.68];
fprintf('Weight\tHeight\n');
fprintf('-----------------\n');
for i=1:length(weights)
    fprintf('%.1f\t%.2f\n', weights(i), heights(i));
end
226 chars
8 lines

In this example, I've created two arrays, weights and heights, containing sample data. Then, I used fprintf() to print the column headers (Weight and Height) and a separator line (-----------------). Finally, I used a for loop to iterate over each element in the arrays and print the values in aligned columns using format specifiers (%.1f for weights and %.2f for heights).

When you run this code, you will see the weight and height data printed in two aligned columns:

main.m
Weight  Height
-----------------
72.5    1.75
68.3    1.62
81.2    1.83
64.7    1.54
76.9    1.68
98 chars
8 lines

gistlibby LogSnag