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

8 views (last 30 days)
Fletcher Roberts
Fletcher Roberts on 13 Sep 2021
Answered: Prateek Rai on 15 Sep 2021
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.
%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 Comment
Stephen23 on 13 Sep 2021
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.

Sign in to comment.

Answers (1)

Prateek Rai
Prateek Rai on 15 Sep 2021
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));




Community Treasure Hunt

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

Start Hunting!