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

2 visualizaciones (últimos 30 días)
Gergely Dolgos
Gergely Dolgos el 22 de En. de 2014
Comentada: Walter Roberson el 22 de En. de 2014
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

Respuestas (3)

AJ von Alt
AJ von Alt el 22 de En. de 2014
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 comentarios
Matt J
Matt J el 22 de En. de 2014
Don't you mean isequal(a,2), Walter?

Iniciar sesión para comentar.


Amit
Amit el 22 de En. de 2014
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 comentarios
Gergely Dolgos
Gergely Dolgos el 22 de En. de 2014
Editada: Gergely Dolgos el 22 de En. de 2014
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 el 22 de En. de 2014
Editada: Amit el 22 de En. de 2014
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'.

Iniciar sesión para comentar.


Matt J
Matt J el 22 de En. de 2014
Editada: Matt J el 22 de En. de 2014
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.

Categorías

Más información sobre Install Products en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by