Info

This question is closed. Reopen it to edit or answer.

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 view (last 30 days)
Kindly help me in rectifing it

Answers (1)

Steven Lord
Steven Lord on 17 Nov 2020
Edited: Steven Lord on 17 Nov 2020
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

This question is closed.

Products

Community Treasure Hunt

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

Start Hunting!