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