Geoscience Reference
In-Depth Information
for i = 1 : size(variables,1)
v = eval(variables{i});
v(isnan(v)==1) = -999;
assignin('base',variables{i},v);
eval(variables{i})
end
clear i v variables
Comparing the variables before and at er the replacement of the
NaN
s
with
-999
reveals that the script works well and that we have successfully
manipulated our data.
h e second important statements to control the l ow of a script (apart
from
for
loops) are
if-then
constructs, which evaluate an expression and
then execute a group of instructions if the expression is true. As an example
we compare the value of two scalars
A
and
B
.
clear
A = 1
B = 2
if A < B
disp('A is less than B')
end
which yields
A is less than B
h e script i rst evaluates whether
A
is less than
B
and, if it is, displays the
message
A is less than B
in the Command Window. We can expand the
if-
then
construct by introducing
else
, which provides an alternative statement
if the expression is not true.
A = 1
B = 2
if A < B
disp('A is less than B')
else
disp('A is not less than B')
end
which yields
A is less than B
Alternatively, we can use
elseif
to introduce a second expression to be
evaluated.
A = 1