Displaying different digits in matrix.
Mostrar comentarios más antiguos
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:

Respuestas (1)
Kristoffer Knudsen
el 6 de Feb. de 2020
Editada: Kristoffer Knudsen
el 6 de Feb. de 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]
Categorías
Más información sobre Characters and Strings en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!