How to replace numbers (-999) for NaN in a dataset?

25 views (last 30 days)
Robert on 12 Mar 2018
Commented: Robert on 12 Mar 2018
I have a large dataset with variables containing -999. I want to change those numbers to NaN but it is not working...
I2=find(Dataset==-999); Dataset(I2)=NaN;
...this is the error I get:
Undefined operator '==' for input arguments of type 'dataset'.
Thank you for your help!

Accepted Answer

Guillaume on 12 Mar 2018
Note: datasets have been deprecated for a while. Consider using tables instead.
A dataset is a collection of variables. So you have to do your replacement per variable. e.g.
yourdataset.somevariable(yourdataset.somevariable == -999) = nan;
%same for all variables
Another option would be to use replacedata. For it to work you'll have to create an m file for a replacement function:
function X = datasetreplace(X)
X(X == -999) = nan;
You can then do:
newdataset = replacedata(yourdataset, @datasetreplace)
Robert on 12 Mar 2018
Hi Guillaume,
Thank you for your help. I am wondering if I could follow with some questions on table? Or should I open another question? as this is a follow up to previous question...
It is OK with you, here is my question. I am trying to use the function grpstats as following:
load Example4Guillaume.mat
Matrix1.Data(Matrix1.Data == -999)=nan;
%creating submatrix and changing variables to nominal
dsa1 = Matrix1(:,{'Location','Depth','Event','Data'});
dsa1.Location = nominal(dsa1.Location);
dsa1.Depth = nominal(dsa1.Depth);
dsa1.Event = nominal(dsa1.Event);
%Applying statarray
statarray = grpstats(dsa1,'Depth');
I am trying to follow ‘hospital’ ( example but getting the following error:
Error using dsgrpstats (line 266)
Data variables must be numeric or logical.
Error in grpstats (line 136)
[varargout{1:nargout}] =
Not sure what I am doing wrong, any advice is welcome!
Attached is a very small example of my very large matrix (Example4Guillaume.mat).

Sign in to comment.

More Answers (1)

KSSV on 12 Mar 2018
Edited: KSSV on 12 Mar 2018
A = [-999 3 4 -999 4 5 -999] ;
B = A ;
B(B==-999) = NaN ;
Don't use find. It will be slow..use logical will be quite fast.
Robert on 12 Mar 2018
Thank you so much KSSV, and sorry I was not clear the first time.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!