Somebody explain me with text base user interface

1 view (last 30 days)
Phat
Phat on 24 Apr 2024
Answered: Saurav on 25 Apr 2024
Hi I'm learning Matlab by myself so when practicing implement unit. I want when the user to input the number of one unit they want to change so which function do I need to use. Here is my code:
%%Conversion unit
fprintf('\t\t\tSIMPLE UNIT CONVERSION PROGRAM\n\n');
%%TEMPERATURE
%% C - F
fprintf('+ TEMPERATURE:\n\t-) C - F:\n\n');
C = input('Enter the value A in Celius: '); %take the value from user
F = input('Enter the value B in Faraheit: ');%take the value from user
fprintf('\n***Apply the equation: (Temperature of F = (Temperature of C + 32) * 5/9) and inverse.\n\n');
toC = (F - 32) * 5/9; %Convert F to C
toF = (C * 9/5) + 32; %Convert C to F
fprintf('After converting the value A Celius to Faraheit: %1.3f F\n', toF); %answer after convert
fprintf('After converting the value B Faraheit to Celius: %1.3f C\n\n', toC);
%%Length & Distance
%% cm - inch
fprintf('+ Length & Distance:\n\t-) cm - inch:\n\n');
cm = input('Enter the value A in centimeter: '); %take the value from user
inch = input('Enter the value B in inch: '); %take the value from user
fprintf('\n***Apply the equation: (Length of cm = Length of inch * 2.54) and inverse.\n\n');
toCM = inch * 2.54; %Convert inch to cm
toINCH = cm / 2.54; %Convert cm to inch
fprintf('After converting the value A centimeter to inch: %1.3f inch\n', toINCH); %answer after convert
fprintf('After converting the value B inch to centimeter: %1.3f cm\n\n', toCM);
%% m - feet
fprintf('\t-) m - feet:\n\n');
m = input('Enter the value A in meter: '); %take the value from users
feet = input('Enter the value B in feet: ');
fprintf('\n***Apply the equation: (Length of m = Length of feet * 0.3048) and inverse.\n\n');
toFEET = m / 0.3048; %Convert meter to feet
toM = feet * 0.3048; %Convert feet to m
fprintf('After converting the value A meter to feet: %1.3f ft\n', toFEET); %answer after convert
fprintf('After converting the value B feet to meter: %1.3f m\n\n', toM);
%% km - miles
fprintf('\t-) km - miles:\n\n');
km = input('Enter the value A in kilometer: '); %take the value from user
mile = input('Enter the value B in mile: ');
fprintf('\n***Apply the equation: (Distance of km = Distance of mile * 1.609344) and inverse.\n\n');
toMILE = km / 1.609344; %Convert kilometer to miles
toKM = mile * 1.609344; %Convert mile to kilometer
fprintf('After converting the value A kilometer to miles: %1.3f mi\n', toMILE); %answer after convert
fprintf('After converting the value B miles to kilometer: %1.3f km\n\n', toKM);
%%Mass
%% grams - ounces
fprintf('+ MASS:\n\t-) grams - ounces:\n\n');
grams = input('Enter the value A in grams: '); %take the value from user
ounces = input("Enter the value B in ounces: ");
fprintf('\n***Apply the equation: (Mass of grams = Mass of ounces * 28.3495) and inverse.\n\n');
toGRAMS = ounces * 28.3495; %Convert ounces to grams
toOUNCES = grams / 28.3495; %Convert grams to ounces
fprintf('After converting the value A gram to ounces: %1.3f oz\n', toOUNCES); %answer after convert
fprintf('After converting the value B ounces to grams: %1.3f gr\n\n', toGRAMS);
%% kg - pound
fprintf('\t-) kg - pound:\n\n');
kg = input('Enter the value A in kiligram: '); %take the value from user
pound = input('Enter the value B in pound: ');
fprintf('\n***Apply the equation: (Mass of kg = Mass of pounds * 0.4536) and inverse.\n\n');
toKG = pound * 0.4536; %convert pound to kilogram
toPOUNDS = kg / 0.4536; %convert kilogram to pound
fprintf('After converting the value A kilogram to poundx: %1.3f lb \n', toPOUNDS); %answer after convert
fprintf('After converting the value B pound to kilogram: %1.3f kg\n\n', toKG);
%% tonne(met) - ton(imp)
fprintf('\t-) tonne(met) - ton(imp):\n\n');
tonne = input('Enter the value A in tonne(met): '); %take the value from user
ton = input('Enter the value B in ton(imp): ');
fprintf('\n***Apply the equation: (Mass of tonne(met) = Mass of ton(imp) * 1.016) and inverse.\n\n');
toTONNE = ton * 1.016; %convert ton to tonne
toTON = tonne / 1.016; %convert tonne to ton
fprintf('After converting the value A ton to tonne: %1.3f tonne(met)\n\n', toTONNE);
fprintf('After converting the value B tonne to ton: %1.3f ton(imp)\n', toTON); %answer after convert
fprintf("Thank you for using the program !!!\n\n")

Answers (1)

Saurav
Saurav on 25 Apr 2024
Hey Phat,
Based on what you have shared, I can understand that you want a function that would help the user choose which particular unit conversion they want to perform among the choices provided in the code.
You can use the “switch, case, otherwise” function in your code to achieve your goal.
Refer to the following documentation to learn how to use the “switch, case, otherwise” function:
Here is an example of how you can incorporate this function into your code:
%%Conversion unit
fprintf('\t\t\tSIMPLE UNIT CONVERSION PROGRAM\n\n');
unit = input('Enter the name of the unit you want to convert ','s')
switch unit
case 'temperature'
%% C - F
fprintf('+ TEMPERATURE:\n\t-) C - F:\n\n');
C = input('Enter the value A in Celius: '); %take the value from user
F = input('Enter the value B in Faraheit: ');%take the value from user
fprintf('\n***Apply the equation: (Temperature of F = (Temperature of C + 32) * 5/9) and inverse.\n\n');
toC = (F - 32) * 5/9; %Convert F to C
toF = (C * 9/5) + 32; %Convert C to F
fprintf('After converting the value A Celius to Faraheit: %1.3f F\n', toF); %answer after convert
fprintf('After converting the value B Faraheit to Celius: %1.3f C\n\n', toC);
case 'length'
%%Length & Distance
%% cm - inch
fprintf('+ Length & Distance:\n\t-) cm - inch:\n\n');
cm = input('Enter the value A in centimeter: '); %take the value from user
inch = input('Enter the value B in inch: '); %take the value from user
fprintf('\n***Apply the equation: (Length of cm = Length of inch * 2.54) and inverse.\n\n');
toCM = inch * 2.54; %Convert inch to cm
toINCH = cm / 2.54; %Convert cm to inch
fprintf('After converting the value A centimeter to inch: %1.3f inch\n', toINCH); %answer after convert
fprintf('After converting the value B inch to centimeter: %1.3f cm\n\n', toCM);
end
fprintf("Thank you for using the program !!!\n\n")
Note: The code provided above serves merely as a reference to demonstrate how this function might be integrated into your own code. It will need to be adapted to fit your specific requirements.
I hope this helps!

Categories

Find more on MATLAB 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!