MATLAB: How to merge two datasets that contains missing values in Statistics Toolbox 7.2 (R2009b)

datasetjoin;keysmergemissingStatistics and Machine Learning Toolboxunique

I want to merge to the following DATASETs and only display necessary information:
D1 = dataset('XLSFile','Ex.xls','Sheet',1,'ReadVarNames',false)
D1 =
Var1 Var2
'A' 1
'B' 2
'C' 3
'E' 11
'H' 17
D2 = dataset('XLSFile','Ex.xls','Sheet',2,'ReadVarNames',false)
D2 =
Var1 Var2
'A' 4
'B' 5
'D' 6
'E' 7

Best Answer

The following example shows how to JOIN the two DATASETs containing missing values and then display only the necessary information:
%%Read data from file as dataset classes
D1 = dataset('XLSFile','Ex.xls','Sheet',1,'ReadVarNames',false);
D2 = dataset('XLSFile','Ex.xls','Sheet',2,'ReadVarNames',false);
%%Join the datasets
J = join(D1,D2,'type','outer','keys','Var1');
%%Look for missing variables
missing = strcmp(J.Var1_left,'');
% Fill in missing values
J.Var1_left(missing) = J.Var1_right(missing);
%%Reduce dataset to show only necessary information
J = dataset(J.Var1_left,J.Var2_left,J.Var2_right) %#ok

%%Convert NaN's to 0 (if necessary)
J.Var2(isnan(J.Var2)) = 0;
J.Var3(isnan(J.Var3)) = 0 %#ok