Info

Questa domanda è chiusa. Riaprila per modificarla o per rispondere.

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 visualizzazione (ultimi 30 giorni)
Kindly help me in rectifing it

Risposte (1)

Steven Lord
Steven Lord il 17 Nov 2020
Modificato: Steven Lord il 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

Questa domanda è chiusa.

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by