How to compare variables without if statement

조회 수: 7 (최근 30일)
new2matlab
new2matlab 2019년 12월 2일
편집: dpb 2019년 12월 3일
I'm looking to verify that two variables produce the same (or different, then an error will occur) result. I'm looking to find a quick way to do this without utilizing if statements.

답변 (1개)

dpb
dpb 2019년 12월 2일
편집: dpb 2019년 12월 3일
>> assert(3==4,'Mismatch')
Mismatch
>> assert(3==3,'Mismatch')
>>
ADDENDUM:
Of course, you can make things more interesting by wrapping the assert inside a try...catch construct...
>> a=3;b=4; % unequal variables
>> try
assert(a==b,'') % check equality, no message output
catch
disp('Unequal') % do whatever on error here
end
Unequal
>>
Of course, what this gains over simply writing a straightforward if...else...end construct is difficult to see without context.
  댓글 수: 2
new2matlab
new2matlab 2019년 12월 2일
Can you explain this?
dpb
dpb 2019년 12월 2일
What's to explain?
> help assert
assert Generate an error when a condition is violated.
assert(EXPRESSION) evaluates EXPRESSION and, if it is false, displays the
error message 'Assertion Failed'.
assert(EXPRESSION, ERRMSG) evaluates EXPRESSION and, if it is false,
displays the character vector contained in ERRMSG. ...
See the doc for all the gory details.
You gave no klews as to what you expect to be able to do...

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

카테고리

Help CenterFile Exchange에서 Debugging and Analysis에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by