Displaying different digits in matrix.

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:
Screenshot_2.png

Answers (1)

You might have some luck with the format command, depending on your needs.
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

Products

Release

R2019b

Tags

Asked:

on 6 Feb 2020

Edited:

on 6 Feb 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!