Code covered by the BSD License

Creating Web Services with MATLAB® Builder™ JA

Kavita Sukerkar (view profile)

18 Oct 2006 (Updated )

A simple temperature conversion web service demonstration using MATLAB® Builder™ JA

convertTemperature(tempIn, tempType)
```function tempOut = convertTemperature(tempIn, tempType)
% tempOut = convertTemperature(tempIn, tempType) converts a set of temperatures in degrees Celsius to degrees Fahrenheit or vice versa.
%     tempIn must contain numeric data, and can be one of the following types:
%          scalar
%          2-dimensional matrix
%          cell array (2-dimensional numeric data or 2-dimensional jagged arrays)
%          struct (must contain a field called 'temperatures' that can be any
%          of the above-metioned types except a struct)
%     tempType indicates the type to convert tempIn to. It can take the
%       following values:
%          'c' or 'C': convert to Celsius
%          'f' or 'F': convert to Fahrenheit
%       tempIn is assumed to be in Celsius format when tempType is 'F'
%       tempIn is assumed to be in Fahrenheit format when tempType is 'C'
%
%  	Example 1:
%       tempIn = 0;
%       tempOut = convertTemperature(tempIn, 'f')
%
%   Example 2:
%       tempIn = [36.9 55.3; 64.2 24.5];
%       tempOut = convertTemperature(tempIn, 'f')
%
%   Example 3:
%       tempIn = {{0}, {1, 2}, {3, 4, 5}};
%       tempOut = convertTemperature(tempIn, 'f')
%
%   Example 4:
%       tempIn = { {36.9, 55.3, 24.5}, {64.2, 78.9, 56.9}, {36.9, 55.3,
%       24.5}, {64.2, 78.9, 56.9} }
%       tempOut = convertTemperature(tempIn, 'f')
%
% Copyright 2006, The MathWorks, Inc.

% non cell arrays
% first check for correct input
if(~isnumeric(tempIn)&&~iscell(tempIn)&&~isstruct(tempIn))
usage;
error('Input must be numeric');
return
end

if(~strcmpi(tempType,'c' ) && ~strcmpi(tempType,'f' ))
usage;
error('tempType must be ''c'' or ''f''');
return
end

if(isstruct(tempIn))
temperatures = [];
try
temperatures = getfield(tempIn, 'temperatures');
tempOutTemp = convertTemperatureGeneral(temperatures, tempType);
tempOut.temperatures = tempOutTemp;

catch
if(~isfield(temperatures))
error('struct inputs must contain a field called temperatures');
else
error(lasterr);
end
return;
end

else
tempOut = convertTemperatureGeneral(tempIn, tempType);
end

function tempOut = convertTemperatureGeneral(tempIn, tempType)
if(isnumeric(tempIn))
classIn = class(tempIn);
tempOut = convertTemperatureMatrix(tempIn, tempType);
tempOut = cast(tempOut, classIn);

% Now for cell arrays
elseif(iscell(tempIn))
% Can be cell arrays containing 2-dimensional numeric data
if(isnumeric(tempIn{1}))
tempIn = cell2mat(tempIn);
classIn = class(tempIn);
tempOut = convertTemperatureMatrix(tempIn, tempType);
tempOut = cast(tempOut, classIn);

% OR there will be second layer to cell arrays (which MUST contain numeric
% data, no more levels of cell arrays are allowed)
% Each element at the second layer can be of the same size or not(jagged)
% Can be jagged
elseif(iscell(tempIn{1}))
[numRows, numCols] = size(tempIn);
currentMatrix=[];
for i=1:numRows
for j=1:numCols
currentMatrix = cell2mat(tempIn{i,j});
tempOut{i,j}=convertTemperatureMatrix(currentMatrix, tempType);
classIn = class(currentMatrix);
tempOut{i,j} = cast(tempOut{i,j}, classIn);
end
end
end
end

% Sub-function that handles conversion for matrices
function tempOut = convertTemperatureMatrix(tempIn, tempType)
tempOut = tempIn;
[numRows, numCols]=size(tempIn);

for i=1:numRows
for j=1:numCols
tempOut(i,j) = convertTemperatureScalar(tempIn(i, j), tempType);
end
end

% Sub-function that handles conversion for scalars
function tempOut = convertTemperatureScalar(tempIn, tempType)
if(lower(tempType)=='f')
tempOut = (tempIn*(9/5)) + 32;
elseif(lower(tempType)=='c')
tempOut = ((tempIn-32)*5/9);
end

function usage