0

I 'm running MATLAB R2016a. I 'm working on a table like this. It contains multiple data types. Fillmissing function runs in R2016b Edition. What I want to achieve is to replace NaN's with blanks only in numerical data types. The goal is to Export a csv without NaN's in numerical columns. Any idea?

    Var1           Var2             Var3
_____________    _________    _____________________

'2101PV'         'QZ'         6,13               
'2102PV'         'QZ'         6,13               
'2103PV'         'QZ'         NaN                  
'2104PV'         'QZ'         NaN    
Dino C
  • 307
  • 3
  • 15

1 Answers1

2
tab(ismissing(tab)) = {''};

But it's not clear how you managed to mix datatypes in your rows...

Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98
  • Thanks for the reply. However, as I mentioned I dont want to drop rows whit nan values. Keep them and replace NaN's with blanks – Dino C Nov 13 '17 at 14:49
  • Edited... this should do the job... but I still can't understand how you managed to mix value types... unless you are using cell arrays with underlying data. – Tommaso Belluzzo Nov 13 '17 at 14:51
  • The reason for this is that I m working on csv files with mix datatypes and somehow some of the blank cells are filled with NaN's. Also, I dont think that isnan can work on a table. It should be converted to an Array, but this works only for the numerical data. – Dino C Nov 13 '17 at 14:55
  • Under Matlab 2016 you can use a more generic ismissing. I edited my answer. rmmissing is another option but not if you want to keep all rows. – Tommaso Belluzzo Nov 13 '17 at 15:13
  • This could work, but only for the char variables. For numerical no. Anyway, thanks for your answers. – Dino C Nov 13 '17 at 15:23
  • No, ismissing is universal. From official docs: "ismissing(A) returns a logical array that indicates which elements of an array or table contain missing values, standard missing values depend on the data type: NaN for double, single, duration, and calendarDuration NaT for datetime for string for categorical ' ' for char {''} for cell of character vectors" – Tommaso Belluzzo Nov 13 '17 at 15:24
  • indeed, ismissing is universal. However, your assignment includes a char value. And for Var3, the assignment gives an error. – Dino C Nov 13 '17 at 15:30
  • 1
    That's why I was wondering about mixed datatypes (what the heck is a NaN doing among char arrays?)... they are gonna produce a real mess. You should sanitize your data before aggregating vars or manipulating them to their final state. detectImportOptions for CSV files could be your best ally. This will probably point you to the right direction: https://mathworks.com/help/matlab/matlab_prog/clean-messy-and-missing-data-in-tables.html – Tommaso Belluzzo Nov 13 '17 at 15:33
  • Ok and mh... and your plan is to convert NaNs in Var3 to empty chars? I don't get the point... – Tommaso Belluzzo Nov 13 '17 at 16:01
  • Yes. The final csv must not include NaN's in the numerical columns. – Dino C Nov 13 '17 at 16:02
  • 1
    You can't... you can't mix types. This is what I'm telling you since the beginning. Either you convert Var3 to string, so you can also allocate empty chars for NaNs, or you keep NaNs. There is no midpoint. – Tommaso Belluzzo Nov 13 '17 at 16:03
  • Ok, if you had only numerical data, what would you suggest? – Dino C Nov 13 '17 at 16:12
  • Have a loot at this: https://stackoverflow.com/questions/45753690/writetable-replace-nan-with-blanks-in-matlab?rq=1 – Tommaso Belluzzo Nov 13 '17 at 16:18