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

Project Euler Problem 67 Solution with Python

Maximum path sum II By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23. 3 7 4 2 4 6 8 5 9 3 That is, 3 + 7 + 4 + 9 = 23. Find the maximum total from top to bottom in triangle.txt (right click and 'Save Link/Target As...'), a 15K text file containing a triangle with one-hundred rows.

Problem 43 Project Euler Solution with python

Sub-string divisibility The number, 1406357289, is a 0 to 9 pandigital number because it is made up of each of the digits 0 to 9 in some order, but it also has a rather interesting sub-string divisibility property. Let d 1 be the 1 st digit, d 2 be the 2 nd digit, and so on. In this way, we note the following: d 2 d 3 d 4 =406 is divisible by 2 d 3 d 4 d 5 =063 is divisible by 3 d 4 d 5 d 6 =635 is divisible by 5 d 5 d 6 d 7 =357 is divisible by 7 d 6 d 7 d 8 =572 is divisible by 11 d 7 d 8 d 9 =728 is divisible by 13 d 8 d 9 d 10 =289 is divisible by 17 Find the sum of all 0 to 9 pandigital numbers with this property. One might write a simple solution using direct if else statements for this problem. Using if else statements, the execution time may be a few seconds. But this is not a very good approach. I too had written a program with if else statement which will take each and every permutation of the 0-9 Pandigital and check for the conditions given in the qu...

Add/Embed SVG to Blogger website

In this post I will tell you my method(trick) of adding SVG images in a blogger website or blog. Before starting , the first thin g I am assu m ing is that you are aware of SVG if you are here. If not please see S calable V ec tor G raphics Recently when I tried to embed a SVG image for a post on pygal, I tried uploading the SVG file and blogger Image uploader came up with an error, because of which I had to find some other way.  SVG File upload Error in Blogger  I started sea rc hing Google " Embed SVG in Blogger " . I found blogorrhea , w h ich gave some i nformatio n on add ing SVG directly as a markup , which worked , but I faced another problem using this . Also th is guy has used lot of Javascript which was confusin g for me, being new to using SVG.   So I first t houg ht of learning on h ow to embed SVG in HTML and t his on e worked out. Actually we can embed SVG in HTML i n following ways: Using Object tag Using Iframe tag Using embed...