Errors Within an If Statement

I keep getting these errors and I'm not sure why. Could somebody check it over and see where I'm exactly going wrong?
function questionsasked()
question1=questdlg('Are you ready to start?',...%question box
'Questions',...
'Yes','No','No');
question2=questdlg('Do you know the keys on the piano?',...
'Questions',...
'Yes','Somewhat','No','No');
question3=questdlg('How did you learn?',...
'Questions',...
'Self-Taught','Lessons','Other','Other')
question4=questdlg('How long did you take lessons for?',...
'Questions',...
'<1 year','1-10 years','>10 years','>10 years')
question5=questdlg('Are you still taking lessons?',...
'Questions',...
'Yes','No','No')
%user must click yes in order to continue
while ~strcmp(question1,'No')%<SM:WHILE>
if strcmp(question1,'Yes')
if strcmp(question2,'Yes')
if strcmp(question3,'Self-Taught')
else strcmp(question3, 'Lessons')
if strcmp(question4,'<1 Year')
if strcmp(question5,'Yes')
else strcmp(question5,'No')
end
else strcmp(question4,'1-10 year')
if strcmp(question5,'Yes')
else strcmp(question5,'No')
end
else strcmp(question4, '>10 years') %error
if strcmp(question5,'Yes')
else strcmp(question5,'No')
end
else strcmp(question3, 'Other')%error
else strcmp(question2,'No')%error
end
end
end
end
question1=questdlg('Are you ready to start?',...%question box
'Questions',...
'Yes','No','No');
end

3 个评论

It looks like you think else is the same as elseif, it isn't. And what error are you getting exactly?
It says parse error at else. But not all of the else's, only that ones that have the error comment. I'm unsure as to what this means.
There are 2 elses:
else strcmp(question3, 'Lessons')
else strcmp(question3, 'Other')%error
You need to change them to an elseif conditional

请先登录,再进行评论。

 采纳的回答

Toder
Toder 2020-5-1
编辑:Toder 2020-5-1
For if elsif else conditionals, Matlab executes the statements in the first block who's logical expression true, then it jumps to end. Only one block of statements is executed. The else is synonymous with "otherwise". It has the statments which are executed if all the previous if and elseif conditionals fail, and only one else can be paired with one if.
Your error is because of the second else. You can have as many elseif conditionals as you like, they just need to appear between the if and the else. I think elseif is what you intend to use in your code. Hopefully this example is helpful.
if x < 5
disp('less than 5')
elseif x > 5
disp('greater than 5')
else
disp('equal to 5')
end
Suppose we set x=6 and then execute this code. Matlab first evaluates the expression x<5, which is not true, so it does not execute disp('less than 5'). Next it evaluates the expression x>5, which is true, so it executes disp('greater than 5') and jumps to end.
Now suppose we instead set x=5. Matlab first evaluates x<5, which is false, so it moves to the elseif conditional. x>5 is false, so it moves to the next block. The last block does not have any expression to evaluate (else statement are NOT allowed to have a logical expression), so disp('equal to 5') is executed, then Matlab jumps to end.

5 个评论

Okay I understand now. Could you answer this question though? It seems that even if i delete the entire if statement the questions still run through. Is this because their at the top of the function? And where should I put this if statement for it to work?
Do you mind clicking accept on my answer since it helped? When the function is called, the code executes from the top down. As it currently is, the user will see the five questions first, and their responses are stored as variables named question1, question2, etc. The while loop then uses the stored responses to do stuff. The order you have it now seems appropriate. You cannot put the while loop above the questions because
while ~strcmp(question1,'No')%<SM:WHILE>
will execute before question1 is defined, and Matlab will throw an error.
Yes of course! And would you mind looking at my code. I think i figured most of it, put some of the graphs won't pop up.
function questionsasked()
question1=questdlg('Are you ready to start?',...%question box
'Questions',...
'Yes','No','No');
if strcmp(question1,'Yes')
question2=questdlg('Do you know the keys on the piano?',...
'Questions',...
'Yes','Somewhat','No','No');
if strcmp(question2,'Yes')
question3=questdlg('How did you learn?',...
'Questions',...
'Self-Taught','Lessons','Other','Other');
if strcmp(question3,'Lessons')
question4=questdlg('How long did you take lessons for?',...
'Questions',...
'<1 year','1-10 years','>10 years','>10 years');
if strcmp(question4,'<1 year')
question5=questdlg('Are you still taking lessons?',...
'Questions',...
'Yes','No','No');
if strcmp(question5,'Yes')
X = categorical({'You','Me'});
X = reordercats(X,{'You','Me'});
Y = [3 5];
bar(X,Y)
else
X = categorical({'You','Me'});
X = reordercats(X,{'You','Me'});
Y = [2 5];
bar(X,Y)
end
elseif strcmp(question4,'1-10 years')
question5=questdlg('Are you still taking lessons?',...
'Questions',...
'Yes','No','No');
if strcmp(question5,'Yes')
X = categorical({'You','Me'});
X = reordercats(X,{'You','Me'});
Y = [4 5];
bar(X,Y)
else
X = categorical({'You','Me'});
X = reordercats(X,{'You','Me'});
Y = [3 5];
bar(X,Y)
end
elseif strcmp(question4,'>10 years')
question5=questdlg('Are you still taking lessons?',...
'Questions',...
'Yes','No','No');
if strcmp(question5,'Yes')
X = categorical({'You','Me'});
X = reordercats(X,{'You','Me'});
Y = [5 5];
bar(X,Y)
else
X = categorical({'You','Me'});
X = reordercats(X,{'You','Me'});
Y = [4 5];
bar(X,Y)
end
elseif strcmp(question2,'No')
X = categorical({'You','Me'});
X = reordercats(X,{'You','Me'});
Y = [0 5];
bar(X,Y)
elseif strcmp(question2,'Somewhat')
X = categorical({'You','Me'});
X = reordercats(X,{'You','Me'});
Y = [1 5];
bar(X,Y)
end
end
end
end
end
Nevermind I actually got it to work, thank you so much for your help though! My ends were in the wrong place.
Toder
Toder 2020-5-1
编辑:Toder 2020-5-1
Glad I could help, and glad you got it working. For the future, if you find yourself using a lot of elseifs, you may want to consider using a switch. You can read about them in the documentation; they are just a lot easier to read than a whole bunch of elseifs.

请先登录,再进行评论。

更多回答(1 个)

Hussein
Hussein 2023-8-20
Please check one of these possible causes
Syntax errors Logical expression errors: Variable scope issues: Data type mismatches: Incorrect nesting or indentation: Typographical errors:

类别

帮助中心File 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