필터 지우기
필터 지우기

I get Error: Invalid expression. Check for missing or extra characters. when commenting out lines in a multi-line statement

조회 수: 14 (최근 30일)
It seems you can't comment-out entire lines in MATLAB within a multi-line statement. Or am I doing it wrong.
A few examples below.
1. Expressions
y = ...
1 + ...
2 + ...
3
Returns:
y =
6
y = ...
1 + ...
%2 + ...
3
Raises:
%2 + ...
Error: Invalid expression. Check for missing or extra characters.
2. Structure definitions
s = struct(...
'A',1,...
'B',2,...
'C',3....
)
Returns:
s =
struct with fields:
A: 1
B: 2
C: 3
struct(...
'A',1,...
% 'B',2,...
'C',3...
)
Raises:
% 'B',2,...
Error: Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched
delimiters.
Even if I use the Comment tool () in the MATLAB editor it causes these errors.
This is such a basic task! What am I doing wrong?

답변 (2개)

madhan ravi
madhan ravi 2020년 8월 25일
You're right.
  댓글 수: 2
Bill Tubbs
Bill Tubbs 2020년 8월 25일
편집: Bill Tubbs 2020년 8월 25일
I wonder whether the ellipsis is even necessary within parenthesis. Python uses '\' for line continuation (ellipsis is used for other things) but it may be omitted within parenthesis:
y = (
1
+ 2
+ 3
)
Anyhow, I'm getting off topic...

댓글을 달려면 로그인하십시오.


Bill Tubbs
Bill Tubbs 2020년 8월 25일
Here is one solution but it's hardly very convenient and contrary to commenting-out in other languages:
y = ...
1 + ...
...%2 + ...
3
Returns:
y =
4
s = struct(...
'A',1,...
...% 'B',2,...
'C',3....
)
Returns:
s =
struct with fields:
A: 1
C: 3
  댓글 수: 2
madhan ravi
madhan ravi 2020년 8월 25일
It is the same as
y = ...
1 + ...
... 2 + ...
3
Which does exactly as the document says that I linked in my answer.
Bill Tubbs
Bill Tubbs 2020년 8월 25일
This is the best solution I think. Don't use '%' at all, just precede any lines you want to comment out with '...'.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by