I'm not sure what im doing wrong on this script...

So, i have to find range and nominal value of resistors with the information from the chart below
Heres my code so far:
first = input('Please enter the first resistance band color: ');
second = input('Please enter the second resistance band color: ');
third = input('Please enter the third resistance band color: ');
toler = input('Please enter the tolerance: ');
nomvalue = (first*10 + second)*10^third;
range = nomvalue + toler*nomvalue;
% Band Colors
black = 0;
brown = 1;
red = 2;
orange = 3;
yellow = 4;
green = 5;
blue = 6;
violet = 7;
gray = 8;
white = 9;
% Tolerance band
none = 0.20;
silver = 0.10;
gold = 0.05;
if first = gray
second = brown
third = black
fourth = none;
fprintf('The nominal value of the resistor is %d \n', nomvalue)
fprintf('The range of the resistor is %d \n', range)
end
I'm not done with it obviously, but its giving me this error:
Error: File: resistors.m Line: 28 Column: 10
The expression to the left of the equals sign is not a valid
target for an assignment.
Heres the directions in case i wasnt clear enough:
Prompt the user for the four colors on the resistor.
-Calculate the nominal value for the resistor and the range of resistance .
-Display (fprintf) the nominal value and range in ohms if the nominal resistance value is smaller than 1000 Ω, in kohms if the nominal resistance value is at least 1000 Ω but less than 1,000,000 Ω and in Mohms if the nominal resistance value is 1,000,000 Ω or higher. Make sure to include units in your fprintf statements. Display two places behind the decimal point for range. Display zero places behind the decimal point if the resistance is in Ω and display one place behind the decimal point if the resistance is in kΩ or MΩ.
-Test your script file using the YELLOW VIOLET ORANGE GOLD example from the previous page to make sure your program is working properly.
I 1. cant figure out how to do the fprintf statement and 2. make the code to where it takes into account ANY color input from the user, not just the ones needed to test the code.
Thank you in advance

댓글 수: 4

The easiest way to handle the fprintf is to use if/elseif/else and have 3 different fprintf.
Remember that MATLAB use == for comparison, not =
Question: is the input numeric band numbers, or is the input character vectors that are color names?
i was planning on using the names of the colors, should i make it a string?
You can either use string objects or character vectors. String objects can be compared using == but character vectors need strcmp() to compare.
input(prompt,'s') returns character vectors.
Hint: Look up tables. ismember() with two outputs.

댓글을 달려면 로그인하십시오.

답변 (1개)

per isakson
per isakson 2019년 3월 10일

0 개 추천

Hint:
%%
C.black = 0;
C.brown = 1;
C.red = 2;
C.orange = 3;
C.yellow = 4;
C.green = 5;
C.blue = 6;
C.violet = 7;
C.gray = 8;
C.white = 9;
%%
first = lower('YELLOW');
second = lower('VIOLET');
third = lower('ORANGE');
%%
R = ( C.(first).*10 + C.(second) ) .* 10^C.(third);
and inspect the result
>> R
R =
47000

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

질문:

2019년 3월 9일

답변:

2019년 3월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by