How to enter simple function for converting Fahrenheit to Celsius?

34 次查看(过去 30 天)
Hi All
I am trying to write a simple function for change of temperature from Fahrenheit to Celsius but i could not. I have been trying to do it from last about 4 hours, and it drives me almost crazy... well what i wrote in m file is
function f = Fahrenheit(c)
% gives temp in Fahrenheit
%converts Celcius to Fahrenheit
f = c*9/5+32
But when i try to run it the commend window gives following error
??? Input argument "c" is undefined.
Error in ==> forenheit at 4
f = c*9/5+32
I am unable to figure out where is the problem. Whereas I have just written two other simple functions successfully. But this one and and another one for calculation of area of circle are not working at all. I shallbe really thankfull for any help

回答(4 个)

Andrei Bobrov
Andrei Bobrov 2012-5-9
CelsiusToFahrenheit = @(c)c*9/5+32
eg
>> CelsiusToFahrenheit(15)
ans =
59

Walter Roberson
Walter Roberson 2012-5-9
You need to store your function in Fahrenheit.m . You currently have it stored in forenheit.m
You cannot run the program by pressing F5. Instead, at the MATLAB command line, you need to call the routine as a function. To use Andrei's sample value, at the command line type in
Fahrenheit(15)
Later when you get the above working, you will need to recode the problem, as your current code converts Celsius to Fahrenheit, whereas your problem description indicates you need to convert the other way around.
  2 个评论
Ahmed Bilal
Ahmed Bilal 2012-5-9
Thank you for you help but still it gives following error
??? Input argument "c" is undefined.
Error in ==> Fahrenheit at 4
f = c*9/5+32
I think the main problem is in input argument "c" I do not know how to define it as the command window is asking
Walter Roberson
Walter Roberson 2012-5-9
How _exactly_ are you running your code? You need to go into the command window and invoke
Fahrenheit(15)
That will temporarily assign the value 15 to c while it runs the code.
If that does not work, please copy your exact current code to here.

请先登录,再进行评论。


Ahmed Bilal
Ahmed Bilal 2012-5-9
@andrei bobrov
Sorry but your answer is not working. When i replaced "c*9/5+32" with "@(c)c*9/5+32" as told by you and when i run the function in command window it returns simply "@(c)c*9/5+32" instead of any value.
  2 个评论
Andrei Bobrov
Andrei Bobrov 2012-5-9
stop!
use code in Command Window:
>> CelsiusToFahrenheit = @(c)c*9/5+32
>> CelsiusToFahrenheit(15)
Walter Roberson
Walter Roberson 2012-5-9
The syntax
CelsiusToFahrenheit = @(c)c*9/5+32
defines "CelsiusToFahrenheit" as a function that does the required conversion work. Just type into the command window the two lines
CelsiusToFahrenheit = @(c)c*9/5+32;
CelsiusToFahrenheit(15)

请先登录,再进行评论。


R ALsabei
R ALsabei 2015-11-27
Create a MATLAB user defined function called Temperature to calculate T in C and F degree.
  1 个评论
Walter Roberson
Walter Roberson 2015-11-27
That does not appear to be a question, it appears to be a command. It is also an unclear command as it does not describe what the inputs are that need to be calculated in C and F degree.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by