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

2 次查看(过去 30 天)
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'.

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 中查找有关 Install Products 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by