Skip to main content

Convert Temperatures - Celsius - Fahrenheit using Matlab

Problem

Ask the user to give the input of a temperature and then again ask the user to give the units of the temperature which was given input before. For now use Celsius and Fahrenheit. If the user has given the input as C then convert from Celsius to Fahrenheit and similarly if the user gives the input as F then convert from Fahrenheit to Celsius. Solve the above problem using Matlab. The conversion factors are as follows:
Conversion from °C to °F
Conversion from °F to °C

Solution 

This program is similar to our program Convert from Degrees Fahrenheit to Degrees Celsius using Matlab. This post can also be said as the back bone for this post or can also be said as an extension to the above post.
This problem is on temperature units conversion. If you want to know more about the units in temperatures then please refer: Wikipedia - Conversion of units of temperature .
This problem is simple and can be solved with basic knowledge in Matlab. Some of the concepts you require to solve the above problem are given below.  I suggest you to see the program first, and then if you have not understood any part then refer to explanation section for detailed explanation about the program. 
1) Request User input - input function
2) if elseif and else in Matlab
3) fprintf in matlab
4) Variables in Matlab
5) Help function in matlab(Optional - Referer Explanation Section)

Program

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
%This program is used to convert the temperature Units
%It will convert from C to F if C is given
%It will convert from F to C if F is given

temperature = input('Please enter the temperature: ');
units = input('Please enter the units of Temperature (C or F): ','s');

if (units == 'F' || units == 'f')
    converted = (temperature-32)*(5/9);
    convertedto = 'C';
elseif (units == 'C' || units == 'c')
    converted = temperature*(9/5) + 32;
    convertedto = 'F';
else
    fprintf('\nYou have given a wrong input\n')
    fprintf('The program will restart again\n')
    fprintf('----------RESTART - FCF.M ---------   \n\n')
    fcf
end

fprintf('%.2f %s = %.2f %s\n',temperature, upper(units),converted,convertedto)

I have saved this program as fcf.m on my local computer.
Refer to the explanation section to know why I have:
1) Given a gap between the first few lines of comments and the original program
2) Why I have used 's' in the input function
3) Why I have used common variables 'converted' and 'convertedto' instead of using c and f to convert the values.

Explanation 

Now lets answer the first question of why I have given a gap between the comments and the program.
We all know that if a program is written, doing documentation for the program is also that important for maintaining the code in the future. So instead of documenting the script in a different file I have done the documentation in the same matlab file. Okay then how can a beginner access my documentation? The answer is very simple. Type the following code in the command prompt:
>> help fcf
You will find that, first three lines of code(i.e. comments) will appear. And thus you have documented the script. Now we can understand from this that when we type help xxxxx, then the first few lines of comments above the gap will get executed. This is the way that all the scripts in the Matlab are documented. Simple isn't it?

Now in lines 5 and 6 we are asking the user to give the input of temperature and the units using the input function. Time to answer question 2. We know that the input function by default assumes that the user is going to give it an input a number. The 's' parameter after the string in the input function tells the computer that the user is going to give an input which is a string. Now even if the user gives a input which is a number, the value is stored as a string. 

Now I have again given a gap in the program on line 7. Does this imply anything here? No there is nothing special about this gap, I have just added a gap to make the code look clean. But one thing is to be remembered, that Matlab will neglect the gaps between the program if any(Except the starting gap for documentation.).

In line 8 I have started the if statement to check the user input. If the user has given an input of 'F' or 'f' then the lines from 9 and 10 gets executed. These lines are the matlab equivalent of the conversion factor given in the question.

In the similar fashion the lines 11-13 containing elseif gets executed but here, the conversion is from 'C' to 'F'. 

Finally if the user gives a wrong input which is not any of these: ['c','C','F','f'], then the else statement of the matlab gets executed and prints that the user has given a wrong input and the program will restart again. To restart the program I have called the fcf.m script from this script(Calling a script from other script is same as typing the name of the script in the Command prompt,which will execute the script).

The conversion is displayed in a neat fashion to the user using the fprintf function

Time to answer the question 3 asked in the program section. If I had used their names like say c and f then to finally display the data of the conversion to the user then I will again have to write the if else statements to check the user input and then display the data. So if the variables are having the same name then I can simply use them in my final fprintf statement. Or else you have one more option. You can simply display the value of the conversion directly after the conversion(i.e. under each if, elseif blocks). Depending upon your choice. 

Try it yourself

1) Modify the code so that there is no fprintf statement in the end and comment you code in the comment box below to help others. (Add the fprintf under each block of if, elseif - refer to the explanation section)
2)  Try with different conversions and comment your output in the comment box below.

Input/Output

Temperature Conversion C to F, F to C using Matlab - fcf.m
Temperature Conversion C to F, F to C using Matlab - fcf.m

Final Note

I have tried to explain everything in this post so that it will be easy for everyone to understand, if you have not understood anything or have a doubt, then please do contact me or comment below. You can contact me from here: Contact me
The above code was high lighted using hilite.me
Keywords: documentation, converstion, c to f, f to c, matlab, temperature, help function

Popular posts from this blog

Making a quiz web app with python and flask

Edit : When you are creating a web app with h tml templates, then y ou will have to sa ve the html file in templates folder in the Current Wor ki ng Directory( CWD). If you save the file in the C W D directl y you will get a TemplateNotFound error. Thank you Udhay for pointing it out.   In this post we will create a quiz website using python . I will be using the flask framework . After reading this tutorial you will learn form submission , flask templates , python code in flask templates , shuffling the questions and options with the random module and few others.  Please note that this tutorial is not big as it seems to be. Some of the code has been rewritten to maintain consistency and also font size is somewhat big so that your eyes won't get stressed reading this tutorial. Also the content has not occupied the full width of the page. In this tutorial I am assuming that you are having a very basic understanding of the flask framework . Please refer the documentation

Problem 11 Project Euler Solution with python

Largest product in a grid In the 20×20 grid below, four numbers along a diagonal line have been marked in red. 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21 24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40 04 52 08 83 97 35 99 16 07

Problem 60 Project Euler Solution with python

Prime pair sets The primes 3, 7, 109, and 673, are quite remarkable. By taking any two primes and concatenating them in any order the result will always be prime. For example, taking 7 and 109, both 7109 and 1097 are prime. The sum of these four primes, 792, represents the lowest sum for a set of four primes with this property. Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime. This problem is j u st a brute force problem. If you have come here because you don't know the limit upto which you will h ave to gener ate the prime numbers t hen go ahe ad and t r y with 10,000 . When I first start ed solving the problem I chose 1 million(beca use most of the problem s on project E uler have this limit ), but it took very long for the computer to fin d the solution. After searching on the internet then I found many people choosing 10, 000 so I have changed my in put f rom 1 million to 10000 and the output was f ast. He