Classify numerical variables into characters using while loops

2 次查看(过去 30 天)
So I have the following matrix volt = [18 33 31 34 15 37 10.5 48 50 38 35 39 42 33 31 1 5 9 13 11 27 35 -1 46 22 6 19 36] and im supposed to cassify them and organize them all into certain names depending on what range the numbers fall in and what their corresponding variable would be using a while loop. The ranges of the variables are as follows; 0 and 10 = A,11 and 20 =P, 21 and 30 = G, 31 and 40 = O and < 0 or > 40 = U. Im also supposed to be able to save the variables in a matrix and display the voltages with their corresponding variable. I created the function bellow but whenever I run it through the driver function it only displays p. The driver function is just calling the function and then displaying the function. The while loop is also only supposed to run until all the values in the volt matrix have been read and assigned. Any help on this would help. Thank you
function [f] = While_Loop(volt)
indx=1;
while indx < 28
if volt(indx)<=10
fruit='A';
elseif volt(indx)<=20
fruit='P';
elseif volt(indx)<=30
fruit='G';
elseif volt(indx) <=40
fruit='O';
elseif (volt(indx)<=0) && (volt(indx) >=40)
fruit='U';
end
indx=indx+1;
end
f=fruit;
end

回答(1 个)

Walter Roberson
Walter Roberson 2020-10-25
Every round of the while loop, you are replacing all of the variable fruit instead of recording the result for each indx .
By the way, I suggest you experiment in your code with the input volt = [48 50 42 46] and see what output you get. And also test volt = [-1 35]

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by