Functions: converting Fahrenheit to Celsius

The question is to write a function f_to_c that accepts a temperature in degrees Fahrenheit and returns the temperature in degrees Celsius. The equation is:
T(in °C) = 5/9[T(in °F) - 32.0]
this is my whole script file and I keep getting this error when I try to use the function
>> f_to_c(100)
Error: File: f_to_c.m Line: 25 Column: 20
Function with duplicate name "f_to_c" cannot be defined.
can anyone tell me what is going on?
function Celsius = f_to_c(Fahrenheit)
Celsius = (5/9)*(Fahrenheit-32);
end

4 Kommentare

which f_to_c -all % what shows up?
Adam
Adam am 18 Mär. 2019
Bearbeitet: Adam am 18 Mär. 2019
That can't be your whole script file if an error is pointing to line 25! Your function should be in its own file.
I changed the file name before and got this when i tried to run the program
>> f_to_c(100)
'f_to_c' is not found in the current folder or on the MATLAB path, but exists in:
Change the MATLAB current folder or add its folder to the MATLAB path.
% Script file: f2c.m
%
% Purpose:
% To convert temperature from degrees Fahrenheit by
% inputting degrees Celsius in MATLAB
%
% Record of revisions:
% Date Programmer Description of change
% ======== ============== =========================
% 03/18/19 S. Butera Ver. 1.0, excercise 6.6
%
%
% Variable Definitions:
% Fahrenheit -- degrees in fahrenheit
% Celsius -- function name
% Clear Workspace
clear;
%declare function
function Celsius = f_to_c(Fahrenheit)
Celsius = (5/9)*(Fahrenheit-32);
end
steven butera
steven butera am 20 Mär. 2019
Sorry I realized I was clearing the workspace and variables, it's working

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Walter Roberson
Walter Roberson am 18 Mär. 2019

1 Stimme

When you define a function inside a script, the function cannot have the same name as the script .m file.
Ahmad Saleh Faraz
Ahmad Saleh Faraz am 26 Sep. 2021

0 Stimmen

lsius = f_to_c(Fahrenheit)
Celsius = (5/9)*(Fahrenheit-32);

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Hilfe-Center und File Exchange

Produkte

Tags

Gefragt:

am 18 Mär. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by