Can I replace NA values with NaN values in an array?
11 views (last 30 days)
Show older comments
I am new to matlab and apologize if this has already been covered... I am working with inputting thirty individual datasets through a previously developed script. Some of these have 'NA' values sprinkled in. I have simple example 4x9 dataset (attached) that has 'NA' values that I would like to replace with 'NaN' values so that Matlab can work with them. I have been using importdata('SCG_2001.csv') which creates a structure made up of one data array and one textdata cell (4x9), which works flawlessly with the script for other datasets (that lack NA values).
I have tried a couple of different solutions unsuccessfully:
1. I used 'TreatAsEmpty' to import the .csv file as a table (with NAs replaced with NaNs) but the rest of my code doesn't work fluidly with a table class.
2. NaN(data.textdata(2:end, 8)) results in the error: "Size inputs must be numeric" and would require me to reset the NaN target to each individual dataset... of which there are 30+...
Simply put, I need to replace any NA values with NaN values within the data.textdata cell but am seriously struggling with making that happen... here is the last attempt at the script that hasn't been working:
data=importdata('SCG_2001.csv')
data(data==NA) = NaN
Undefined function or variable 'NA'.
The sample dataset is attached. Thank you for your help!
0 Comments
Accepted Answer
Rik
on 10 Aug 2018
The code below will look for any cells that are equal to 'NA' and replace them with NaN.
data=importdata('SCG_2001.csv');
tf=cellfun(@(x)strcmp(x,'NA'),data.textdata);
data.textdata(tf)={NaN};
More Answers (0)
See Also
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!