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

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...

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...

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 documenta...