How to format an array so all numbers are the same length
Show older comments
I have a piece of code that outputs a table. I would like for the vertical lines of each column to line up but I don't know how to format the numbers so that they are all of the same length
| Volume | Mass | Force |
Tube A | 0.000216| 0.561 |5.4989|
Tube B | 0.000698| 1.815 |17.8038|
Tube C | 0.000477| 1.239 |12.1546|
Tube D | 0.000298| 0.774 |7.5933|
1 Comment
per isakson
on 28 Mar 2021
I guess that you use the function, fprintf(). Did you read the section on formatspec in the help of fprintf()?
Accepted Answer
More Answers (1)
data = repmat([0.01, 0.1, 1, 10, 1000], 2, 1);
fprintf(' |%8.6g| %-8.2f |\n', data)
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!