MATLAB Answers

Why do I get ans = logical 1 or 0 in the command window for the following script?

9 ビュー (過去 30 日間)
Left Terry
Left Terry 2021 年 7 月 29 日
コメント済み: Left Terry 2021 年 7 月 29 日
%Pythagorean Theorem
clc, clear all, disp('Pythagorean Theorem Formula c = sqrt(a^2+b^2)')
a=input('Enter length a: a = ');
b=input('Enter length b: b = ');
if(a>0), (b>0)
c=sqrt(a^2+b^2)
else
disp('WARNING! Enter only positive lengths')
end

採用された回答

Rik
Rik 2021 年 7 月 29 日
A comma does not mean and. In the Matlab syntax it is used to delimit two statements, so the second part of the line with your if statement is executed if a is larger than 0, in which case it will print the result of b>0 to the command window, because you didn't use a semicolon to suppress the output.
The mlint will have warned you about this with the orange line below the >. You should use the help mlint is giving you. Make sure to deal with all the warnings it is giving you. It will also tell you that clear all should be avoided.
  1 件のコメント
Left Terry
Left Terry 2021 年 7 月 29 日
Thank you very much! I solved it. It now works perfectly.

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

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by