Problem
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)
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.
2) Try with different conversions and comment your output in the comment box below.
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 |
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