excel - writetable replace NaN with blanks in Matlab -
given matlab table contains many nan
, how can write table excel or csv files nan replaced blanks?
i use following function:
t = table(nan(5,2),'variablenames',{'a','c'}) writetable(t, filename)
i not want replace zeros. want output file:
- has blanks nan ,
- that variable names included in output.
you need xlswrite
that. replaces nan
s blanks itself. use table2cell
or combination of table2array
, num2cell
convert table cell array first. use variablenames
property of table retrieve variable names , pad them cell array.
data= [t.properties.variablenames; table2cell(t)]; %or data= [t.properties.variablenames; num2cell(table2array(t))]; xlswrite('output',data);
sample run for:
t = table([1;2;3],[nan; 410; 6],[31; nan; 27],'variablenames',{'one' 'two' 'three'}) t = 3×3 table 1 2 3 ___ ___ _____ 1 nan 31 2 410 nan 3 6 27
yields:
although above solution simpler in opinion if want use writetable
then:
tmp = table2cell(t); %converting table cell array tmp(isnan(t.variables)) = {[]}; %replacing nan entries [] t = array2table(tmp,'variablenames',t.properties.variablenames); %converting table writetable(t,'output.csv'); %writing csv file
Comments
Post a Comment