Azzera filtri
Azzera filtri

problem with while and if loops.

1 visualizzazione (ultimi 30 giorni)
Andrew
Andrew il 30 Set 2023
Risposto: Walter Roberson il 30 Set 2023
Im having an issue where when the user inputs 'no' as the user_status variable i get an error that the arrays are no longer compatible. If I solely stick to userstatus being yes everything works perfect. The only code in question is up to the end of the if statement as the function has nothing to do with the error I believe. How can I get this error to go away?
user_status = input('would you like to find your resistor value? enter yes or no with an apostrophe on each side of the text');
while user_status=='yes'
[resistance,tolerance] = resistor_value();
user_status = input('would you like to find another resistor value? enter yes or no');
end
if user_status=='no'
user_status = input('if you would like to try again type yes');
end
%% function not important as it deals with something irrelevant
function [resistance,tolerance] = resistor_value()
first_band = {'black','brown','red','orange','yellow','green','blue','violet','grey','white'};
second_band = {'black','brown','red','orange','yellow','green','blue','violet','grey','white'};
multiplier_band = {'black','brown','red','orange','yellow','green','blue','violet','grey','white','gold','silver'};
tolerance_band = {'brown','red','orange','yellow','green','blue','violet','grey','gold','silver'};
disp(first_band)
disp('type the index in the string above of the color that correlates to your resistor starting with 0 as black, 1 as brown, and so forth')
band1num = input('your index number for band 1 is');
disp(second_band)
disp('type the index in the string above of the color that correlates to your resistor starting with 0 as black, 1 as brown, and so forth')
band2num = input('your index number for band 2 is');
disp(multiplier_band)
disp('type the index in the string above of the color that correlates to your resistor starting with 0 as black, 1 as brown, and so forth')
band3num1 = input('your index number for the multiplier band is');
band3num2 = 10^band3num1;
disp(tolerance_band)
disp('type the index in the string above of the color that correlates to your resistor starting with 0 as brown, 1 as red, and so forth')
band4num = input('your index number for the tolerance band 4 is ');
band4str = {'1%','2%','.05%','.02%','.5%','.25%','.1%','.01%','5%','10%'};
band4value = band4str{band4num};
resistance = ((band1num*10)+band2num)*band3num2;
disp('resistance is')
disp(resistance)
tolerance = band4value;
disp('tolerance is')
disp(tolerance)
end

Risposta accettata

Walter Roberson
Walter Roberson il 30 Set 2023
user_status = input('would you like to find your resistor value? enter yes or no with an apostrophe on each side of the text');
You are expecting the user to type something that evaluates to a character vector. (Note that you could use the 's' option of input() so that the user does not need to put in the apostrophes themselves.)
while user_status=='yes'
You are trying to use == to compare the character vector that was input to the character vector 'yes'
For this purpose it is important that the way that MATLAB represents character vectors is as vectors of characters. So 'no' is not represented as a single undivided entity in MATLAB: instead it is implemented as ['n' 'o'] -- a vector with two positions. And then when you compare that with == to 'yes' you are effectively testing
['n' 'o'] == ['y' 'e' 's']
since characters are represented as 16 bit unsigned integers, that is the same as if you had tested
[110 111] == [121 101 115]
because 'n' is represented as the 16 bit integer 110 and 'o' is represented as 111 and so on. The exact representation of characters to numbers is as given by the Unicode Consortium, a key international standard.
You might have recognized by now that using == to compare a vector with 3 elements to a vector with two elements is not a supported operation in MATLAB. When you use == to compare vectors, either they have to be exactly the same size or else one of the two must be a scalar.
What can you do? Well, the recommended approach is to use strcmp or strcmpi ... or to avoid the problem by using menu or listdlg or questdlg or uiconfirm

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by