Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Error: File: testem.m Line: 87 Column: 5 Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses.

조회 수: 1 (최근 30일)
Kindly help me in rectifing it

답변 (1개)

Steven Lord
Steven Lord 2020년 11월 17일
편집: Steven Lord 2020년 11월 17일
If you're going to put the end keyword (or the elseif keyword that's part of an if statement) on the same line as another statement, separate them with either comma or semicolon.
for k = 1:10, if k == 5, disp('Hello'), elseif k == 7, disp('Goodbye'), end, end % works
% What follows is the output of the commands above
Hello
Goodbye
Compare this with the version without commas.
for k = 1:10 if k == 5 disp('Hello') elseif k == 7 disp('Goodbye') end end % will not work
% What follows is the text of the error when I tried running the previous line
for k = 1:10 if k == 5 disp('Hello') elseif k == 7 disp('Goodbye') end end % will not work
Error: Illegal use of reserved keyword "elseif".
My personal preference is not to write multiple statements on one line like this. The following does take up more vertical real estate on screen, but it also (IMO) makes it easier to understand what end goes with what other keyword.
for k = 1:10
if k == 5
disp('Hello')
elseif k == 7
disp('Goodbye')
end
end

이 질문은 마감되었습니다.

제품

Community Treasure Hunt

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

Start Hunting!

Translated by