Can I replace NA values with NaN values in an array?

10 vues (au cours des 30 derniers jours)
Zachary Miller
Zachary Miller le 10 Août 2018
Commenté : Zachary Miller le 13 Août 2018
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!

Réponse acceptée

Rik
Rik le 10 Août 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};
  1 commentaire
Zachary Miller
Zachary Miller le 13 Août 2018
Worked perfect! Thank you very much!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Tables dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by