% R_code.m       Convert three strings containing colors of bands on R to R value.
%
% Inputs:
%  color1 = 1st band color (string variable)
%  color2 = 2nd band color
%  color3 = 3rd band colar % We use separate variables because strings may be of
                           % different lengths.
%
% Output:
%  R_value = numerical value of resistance in ohms.

% We use a loop to convert the three colors to three numbers.
for band_num = 1:3     % Three color bands

  % Use 'if' statements to set 'color' variable.
  if band_num == 1
    color = color1;
  elseif band_num == 2
    color = color2;
  else
    color = color3;
  end   % end of if statement.

  % Use a switch statement to set value for color band.
  switch color     % 'color' is the variable being evaluated
    case 'black'   % Equivalent to if color == 'black'
      value(band_num) = 0;   % Index by band_num so 3 different band values can be set.
    case 'brown'  % Unlike the C language, this code is not executed if the 'black'
                  %  section is executed.
      value(band_num) = 1;
    case 'red'
      value(band_num) = 2;
    case 'orange'
      value(band_num) = 3;
    case 'yellow'
      value(band_num) = 4;
    case 'green'
      value(band_num) = 5;
    case 'blue'
      value(band_num) = 6;
    case 'purple'
      value(band_num) = 7;
    case 'gray'
      value(band_num) = 8;
    case 'white'
      value(band_num) = 9;
    otherwise       % If all else fails, print an error message and exit.
      display('color not found')
      return
  end              % Terminates the switch statement

end    % end of 'for' loop.

% We could use if statements instead of the switch statement, but it would take more
%  typing.

% At this point we should have values in value(1) thru value(3) for the 3 bands.
% Convert the digits into a resistor value.
R_value = value(1:2)*[10,1]'*10^value(3)
