Displaying different digits in matrix.
Show older comments
Current code :
clc
clear
theta=[50 60 70 80];
thetarad=theta*pi/180;
v=[10 12 14 16 18 20];
h=((v.^2)'*(sin(thetarad)).^2)/(2*9.81);
h1=round(h,1);
tab=[[0,theta];v',h1];
disp(tab)
Disarable result:

Answers (1)
Kristoffer Knudsen
on 6 Feb 2020
Edited: Kristoffer Knudsen
on 6 Feb 2020
You can put your numbers in a table, allowing you to format the headers (frozen as strings) apart from the data (formatted according to context):
format short % Five digits
t = array2table(h1, 'VariableNames', string(theta), 'RowNames', string(v))
However, to my knowledge, Matlab does not allow user-specified formats for disp. If you have specific needs (such as 'exactly a single number following the decimal point'), you'll have to format the strings yourself.
Creating an array of formatted strings can be done like so:
% Create an array of strings; each string a number formatted appropriately.
s = arrayfun(@(h) sprintf("%0.1f", h), h1)
tab = [string([0, theta]); ...
string(v'), s]
Categories
Find more on Characters and Strings 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!