if loop error in MALAB

4 ビュー (過去 30 日間)
ali hassan
ali hassan 2022 年 2 月 2 日
コメント済み: Steven Lord 2022 年 2 月 2 日
i am using the following if loop but it is not executing:
n=[]
if n==[]
sprintf('a')
end
it dose not print 'a'
  2 件のコメント
ali hassan
ali hassan 2022 年 2 月 2 日
編集済み: ali hassan 2022 年 2 月 2 日
but why it is not working that way?@James Tursa
Steven Lord
Steven Lord 2022 年 2 月 2 日
From the documentation page: "if expression, statements, end evaluates an expression, and executes a group of statements when the expression is true. An expression is true when its result is nonempty and contains only nonzero elements (logical or real numeric). Otherwise, the expression is false."
Is your expression nonempty and does it contain only nonzero elements?
n = [];
n == []
ans = 0×0 empty logical array
Your expression is empty so the statements inside the if block are not executed.
If you look at the suggestion from James Tursa:
isempty(n)
ans = logical
1
This is nonempty and it contains only nonzero elements. Therefore the if statement's expression is true and so the statements inside the if block are executed.

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

採用された回答

James Tursa
James Tursa 2022 年 2 月 2 日
Change your test to
if isempty(n)

その他の回答 (0 件)

カテゴリ

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