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

5 ビュー (過去 30 日間)
Hello There
Hello There 2019 年 3 月 9 日
回答済み: per isakson 2019 年 3 月 10 日
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 件のコメント
Hello There
Hello There 2019 年 3 月 9 日
i was planning on using the names of the colors, should i make it a string?
Walter Roberson
Walter Roberson 2019 年 3 月 9 日
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 日
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

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by