MATLAB: How to write a script file that asks the user to input the total resistance for a resistor and then outputs a list of colors for the bands in order (1, 2, 3)

bandscolorsif statementresistancescr

A resistor has three colored bands that shows its total resistance. The total resistance = ((color of first band)*10 + (color of second band))*10^(color of the third band)
black is 0
brown is 1
red - 2
orange - 3
yellow - 4
green - 5
blue - 6
violet - 7
gray - 8
white -9

Best Answer

This takes me back to my amateur radio days (although I was used to mapping from stripes to resistance):
Colours = {'black' 'brown' 'red' 'orange' 'yellow' 'green' 'blue' 'violet' 'gray' 'white'};
Values = [0:9];
Rc = inputdlg('Stripe your resistor! What is its resistance (Ω)? ')
Rstr = Rc{:};
for k1 = 1:length(Rstr)
parse{k1} = Rstr(k1);
end
Mult = fix(log10(str2num(Rstr)));
Bands = parse(1:2);
Resistor = Colours([str2num(Bands{1})+1, str2num(Bands{2})+1, Mult])
fprintf(1, '\tYour resistor of %s Ohms will be striped %s, %s, %s\n\n', Rstr, char(Resistor{1}), char(Resistor{2}), char(Resistor{3}))
Example output:
Your resistor of 47000 Ohms will be striped yellow, violet, orange