How do I make a variable show up as inches, so that I may then change it to cm?

8 次查看(过去 30 天)
I have to make my variables inches and then convert to cm but I do not know how to make them show up as a defined unit of length at all.
(x=4,y=5,z=6)
%define the lengths
x=input ('Enter the lenth of side x: ')
y=input ('Enter the length of side y: ')
z=input ('Enter the length of side z: ')
%define s
s = (x+y+z)./2;
%this is the area
area = sqrt(s*(s-x)*(s-y)*(s-z));
  1 个评论
Stephen23
Stephen23 2021-9-13
The integer and floating point numeric data types that your computer supports do not store any units.
If you really must store units as part of the data stored in memory (which I very much doubt is the case) then you can add them as extra data, e.g. in a structure or via a custom data class, you can find some on FEX:
Most likely your assignment is just asking you to scale the values by the ratio of the units.

请先登录,再进行评论。

回答(1 个)

Prateek Rai
Prateek Rai 2021-9-15
To my understanding, you want to make the variables in inches and then convert them to cm and use it in your calculation.
Probably the easiest way is to clearly mention that input should be in inches. Then you can convert it to cm to use it in calculation. One more thing you can add to clearly distinguish whether the variable is using cm or inches is to add some indication in the variable name itself as "variable_name_inch" or "variable_name_cm".
So probably the code should look like this:
% Taking input from user and clearly mentioning it to be in inches
x_inch = input ('Enter the length of side x (input should be in inches) : ')
y_inch = input ('Enter the length of side y (input should be in inches) : ')
z_inch = input ('Enter the length of side z (input should be in inches) : ')
% Converting to cm
conversion = 2.54;
x_cm = x_inch * conversion;
y_cm = y_inch * conversion;
z_cm = z_inch * conversion;
% Now you can use x_cm, y_cm and z_cm in the rest part of your code
%define s
s = (x_cm + y_cm + z_cm)./2;
%this is the area
area = sqrt(s*(s-x_cm)*(s-y_cm)*(s-z_cm));

类别

Help CenterFile Exchange 中查找有关 Matched Filter and Ambiguity Function 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by