Removing NaNs from all Variables in the Workspace

Have you ever asked yourself how to automatically remove all NaNs (not a number) in all variables in the workspace automatically? Here is a small MATLAB script for this.

I often had the problem that I had a lot of variables of different types and dimensions in the workspace, which contain either NaN or another no data identifier to identify data gaps. If you want to use this data with other software tools, such as ArcGIS, NaNs must be replaced by other no-data identifiers. The other way round, if you work with digital terrain models (such as the SRTM data set, see Chapter 7.5 of the MRES book), you have to place -32768 (i.e. the lowest possible value of data of the signed integer 16 bit or int16 format) by NaNs in order to use them with MATLAB. As an example, we first create some random variables with NaNs:

clear
A = rand(3,3); A(2,1) = NaN;
BC = rand(2,4); BC(2,2) = NaN;
DE = rand(1,2); DE(1,1) = NaN;
FG = rand(3,2); FG(2,2) = NaN;
HJ = 'A character array';

We can display the value of the variables in the Command Window by typing

A, BC, DE, FG, HJ

resulting in the output

A =
    0.9797  0.2581  0.2622
    NaN     0.4087  0.6028
    0.1111  0.5949  0.7112
BC =
    0.2217  0.2967  0.4242  0.0855
    0.1174  NaN     0.5079  0.2625
DE =
    NaN     0.0292
FG =
    0.9289  0.5785
    0.7303  NaN
    0.4886  0.4588
HJ = A character array

Here is the script to replace all NaNs by the value -999. It first stores the list of variables from the output of who in the array variables. Then it uses eval to execute the MATLAB expression in the variable names, i.e. it gets the values of the variables by calling them, and stores the variables in the array v. Then it locates the NaNs using isnan and replaces the NaNs by -999. Then it assigns the new arrays to the variables variables. Finally it displays the new values of the variables using eval.

variables = who;
for i = 1 : size(variables,1)
    v = eval(variables{i});
    v(isnan(v)==1) = -999;
    assignin('base',variables{i},v);
    eval(variables{i})
end

resulting in the output

A =
    0.1690    0.6477  0.2963
    -999.0000 0.4509  0.7447
    0.7317    0.5470  0.1890
BC =
    0.6868    0.3685  0.7802  0.9294
    0.1835 -999.0000  0.0811  0.7757
DE =
    -999.0000 0.4359
FG =
    0.4468     0.5108
    0.3063  -999.0000
    0.5085     0.7948
HJ =
    A character array

You can use the script to replace any other value by another value.