why does this line of code give error- disp("The density of " + element ... + "is" + density). When i press the 'enter ' key after the 3 dots and rerun, it works.

1 回表示 (過去 30 日間)
funmilayo
funmilayo 2024 年 8 月 18 日
編集済み: Stephen23 2024 年 8 月 18 日
if doPlot == 1
plot(density)
title("Sample Densities")
xticklabels(element)
ylabel("Density (g/cm^3)")
else
disp("The Density of " + element ... + "is" + density)
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
end

採用された回答

Shivansh
Shivansh 2024 年 8 月 18 日
Hello funmilayo,
The error you're encountering is due to the way MATLAB handles line continuations. In MATLAB, the symbol (...) is used to indicate that a statement continues on the next line. However, if you press the 'Enter' key after the ellipsis, it correctly interprets the continuation.
You can modify your code as shown below to avoid the error:
if doPlot == 1
plot(density)
title("Sample Densities")
xticklabels(element)
ylabel("Density (g/cm^3)")
else
disp("The Density of " + element ...
+ " is " + density)
end
I hope it resolves the query!
  2 件のコメント
funmilayo
funmilayo 2024 年 8 月 18 日
Hi,
Thank you, i understand now.
Yes, i did press the enter key and i noticed it worked. That was why i was confused because i just replicated the code as shown in my Onramp task and it gave an error.
The point of seeking clarification is to understand what the 3 dots (ellipsis) means in matlab so i do not make the same mistake going forward as i am new to matlab.
Stephen23
Stephen23 2024 年 8 月 18 日
編集済み: Stephen23 2024 年 8 月 18 日
"The point of seeking clarification is to understand what the 3 dots (ellipsis) means in matlab..."
Ellipsis is explained here:

サインインしてコメントする。

その他の回答 (1 件)

John D'Errico
John D'Errico 2024 年 8 月 18 日
編集済み: John D'Errico 2024 年 8 月 18 日
Do this instead:
disp("The Density of " + element + " is " + density)
What did you write?
disp("The Density of " + element ... + "is" + density)
This statement is incomplete.
This must fail, because you stuffed that ... line continuation flag in there. But you did not actually split the line of code onto a second line. That confuses MATLAB. Do you see the second half of the line is green? That tells you MATLAB apparently thinks that second part of the line after the ellipsis is a comment. And now it thinks the line is incomplete, that a closing parens is missing, etc. Remember: anything in green was interpreted as a comment.
If you wanted to put the ellipsis inside quotes, to be displayed, that would work. But computers are very easily confused.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by