while loops. Why is it not stopping

19 views (last 30 days)
hi all i am doing a while loops, and i want it to stop when i entered the number 1 or the word load the data, but it doesn't want to stop when i enter the number 1 but it works fine when i enter the word 'load the data'. Plz check it out for me.
questionLine{1} = 'Access the feature by entering the number or the word?'
questionLine{2} = '1. Load the data';
questionLine{3} = '2. Load new data';
questionLine{4} = '3. Data fitting';
questionLine{5} = '4. Filter data';
questionLine{6} = '5. quit';
while (~isempty( choicenum) && choicenum~=1) strcmpi(choiceStr,'load the data')~=1 % when choicenum is not 1 or choicestr is not load the data. run the while loops.
question = [1:2,6]; % displaying the options.
for i = question
disp(questionLine{i})
end % just printing the questionLines.
choiceStr=input('Enter the word or the number:','s');
choicenum=str2num(choiceord);
disp('Enter a valid number or word')
end

Accepted Answer

Geoff Hayes
Geoff Hayes on 21 Jun 2014
One problem is that there are two conditions to the while loop: one to check on the number and the other to check on the string, but there is no && or operator between these two conditions. It should probably read instead
while (~isempty(choicenum) && choicenum~=1) && strcmpi(choiceStr,'load the data')~=1
which would mean if the choice (as a number) is non-empty and not equal to one AND the choice as a string is not equal to "load the data" then continue looping. But I don't think this is quite what you want.
It may be easier to think of it in the other direction - quit the loop if the user selects a certain number (so choicenum is non-empty and choicenum==x) OR if he/she enters a certain string
((~isempty(choicenum) && choicenum==1) || strcmpi(choiceStr,'load the data')==1)
As the above statement would be true if the user selects one or types in 'load the data' then the while loop would finish. We want to continue the loop so long as this statement is NOT true. So just negate it
while ~((~isempty(choicenum) && choicenum==1) || strcmpi(choiceStr,'load the data')==1)
Since choicenum and choiceStr are used in the condition, they will have to be defined prior to the while loop. And so your code becomes
choiceNum = 0;
choiceStr = '';
exitId = 1;
while ~((~isempty(choiceNum) && choiceNum == exitId) || strcmpi(choiceStr,'load the data')==1)
question = [1:2,6]; % displaying the options.
% print the questions
for i = question
disp(questionLine{i})
end
choiceStr=input('Enter the word or the number:','s');
choiceNum=str2num(choiceStr);
end
Note the slight changes to your original code - the removal of disp('Enter a valid number or word'), the renaming of choicenum to choiceNum, and the replacing of choiceord with choiceStr.
I suspect you also want to change the exit condition, exitId, to 5 since that is the menu option to exit or quit the loop. The corresponding 'load the data' string would have to change to 'quit'.
Try the above and see what happens!

More Answers (1)

Image Analyst
Image Analyst on 21 Jun 2014
How about
button = 1
while button == 1
button = menu('What do you want to do?', 'Load the data', 'Quit');
% Code to load the data, etc.
end
  2 Comments
Jian Gui
Jian Gui on 21 Jun 2014
it is an assignment and i am not allow to use the 'menu' function:(
Image Analyst
Image Analyst on 21 Jun 2014
But you're allowed to use input(), str2num(), disp(), etc. in your homework solution? Why? What's so bad about menu() and not the others????

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!