will "if exist('a','var') && a == 2" always work, without throwing an exception?

조회 수: 7 (최근 30일)
Gergely Dolgos
Gergely Dolgos 2014년 1월 22일
댓글: Walter Roberson 2014년 1월 22일
In most cases this is what happens with this if construct:
clear all
% first case
if exist('a','var') && a==2
disp('if went');
else
disp('if did not go');
end;
output: if did not go
% second case
if a==2 && exist('a','var')
disp('if went');
else
disp('if did not go');
end;
output: Undefined function or variable 'a'.
However, i want to convince my coworkers to not use it this way, because it has given me a problem even in the first case, by throwing " undefined function or variable 'a' ".
Is it therefore necessary to use it the way you can see below, in order to never have a problem?
if exist('a','var')
if a==2
disp('a is defined and a is 2');
else
disp('a is defined, a is not 2')
end
disp('a is not defined')
end

답변 (3개)

AJ von Alt
AJ von Alt 2014년 1월 22일
All of these approaches will throw an error if a is not a numeric array, logical array, character array, or categorical array. The operator == is only defined for these types of inputs.
For example if a is a cell array, the following code:
a = {};
if exist( 'a' , 'var' ) && a == 2
end
Will throw the error:
Undefined function 'eq' for input arguments of type 'cell'.
  댓글 수: 3
Matt J
Matt J 2014년 1월 22일
Don't you mean isequal(a,2), Walter?

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


Amit
Amit 2014년 1월 22일
I don't see a question. You code is fine.
Wouldn't it be a programmer's choice and will depend on the purpose of the variable 'a'. Lets say you don't care about 'a' unless it is equal to 2, then why going through more if statements.
Like in the initial case, you must have learned this fairly quickly the difference between
exist('a','var') && a==2 or a==2 && exist('a','var')
  댓글 수: 2
Gergely Dolgos
Gergely Dolgos 2014년 1월 22일
편집: Gergely Dolgos 2014년 1월 22일
it depends on if the logical short circuiting is invoked
if it somehow does not invoke the short circuiting behavior, then it is wrong
Amit
Amit 2014년 1월 22일
편집: Amit 2014년 1월 22일
I do understand logical short circuit. My comment was regarding your question on the universality of your approach.
My point was that with time (after few errors) you realized that one way of short circuiting is right versus the other way. And you wont do that mistake. Both approaches will give you the same result. However, one approach is playing safe and the other one needs one to know what they are doing. Isn't it?
There are many questions on MAtlab Answers where people want to reduce the number of 'if ..end'.

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


Matt J
Matt J 2014년 1월 22일
편집: Matt J 2014년 1월 22일
However, i want to convince my coworkers to not use it this way, because it has given me a problem even in the first case, by throwing " undefined function or variable 'a' ".
The first version should never have given you any problems. In fact, on any MATLAB version in recent memory, it shouldn't have even given you problems if you had used a normal & like below instead of an explicit short-circuited &&
if exist('a','var') & a==2 %short circuits
disp('if went');
else
disp('if did not go');
end;
Your second version would always create problems if 'a' doesn't exist as a variable. Because logical operations are done left-to-right, you aren't giving the '&&' a chance to short-circuit.

카테고리

Help CenterFile Exchange에서 Language Support에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by