if loop error in MALAB
조회 수: 2 (최근 30일)
이전 댓글 표시
i am using the following if loop but it is not executing:
n=[]
if n==[]
sprintf('a')
end
it dose not print 'a'
댓글 수: 2
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 == []
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)
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.
채택된 답변
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!