Home > published > tempconvert.m

tempconvert

PURPOSE ^

tempconvert Takes input in degrees C, K, F, or R.

SYNOPSIS ^

function [ Tout ] = tempConvert( Tin,inDegrees,outDegrees )

DESCRIPTION ^

tempconvert Takes input in degrees C, K, F, or R.
Returns type selected.
USAGE:
tempConvert([0 32 273 298],'k','f')
ans =
 -459.4000 -401.8000   32.0000   77.0000

history: v1.0 by R.Sonnenfeld, 9/4/2006

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

function [ Tout ] = tempConvert( Tin,inDegrees,outDegrees )
%tempconvert Takes input in degrees C, K, F, or R.
%Returns type selected.
%USAGE:
%tempConvert([0 32 273 298],'k','f')
%ans =
% -459.4000 -401.8000   32.0000   77.0000
%
%history: v1.0 by R.Sonnenfeld, 9/4/2006
global DEBUG
DEBUG=0;
inDegrees=upper(inDegrees);
%Convert strings to upper case to make fewer case statements
outDegrees=upper(outDegrees); 
if DEBUG==1 
    fprintf('Input temperature was %6.1f\n',Tin); 
    fprintf('Units input are %3s\n',inDegrees);
    fprintf('Units requested are %3s\n',outDegrees);
end     

switch inDegrees
    %Although it is slightly less "efficient", it is easier to convert all
    %input temperatures to Kelvin, then reconvert them to the selected
    %output scale.
    case('K')
        K=Tin;
    case('C')
        K=Tin+273;
    case('R')
        F=Tin-459.67;
        K=((5/9)*(F-32))+273;
    case('F')
        K=((5/9)*(Tin-32))+273;
    otherwise
        warning(['Invalid input units ',inDegrees])
end
if DEBUG==1 fprintf('Temperature in Kelvin is %6.1f\n',K); end       
switch outDegrees
    %Temperature has already been converted to Kelvin
    %Now convert it to selected scale for output.
    case('K')
        Tout=K;
    case('C')
        Tout=K-273;
    case('F')
        Tout=(9/5)*(K-273)+32;
    case('R')
        F=(9/5)*(K-273)+32;
        Tout=F+459.67;
    otherwise
        warning(['Invalid output units ',outDegrees])
end

Generated on Tue 07-Nov-2006 10:13:02 by m2html © 2005