您只需xlswrite https://www.mathworks.com/help/matlab/ref/xlswrite.html为了那个原因。它取代了NaN
s 本身带有空格。使用table2cell https://www.mathworks.com/help/matlab/ref/table2cell.html或组合table2array https://www.mathworks.com/help/matlab/ref/table2array.html and num2cell https://www.mathworks.com/help/matlab/ref/num2cell.html首先将表转换为元胞数组。使用VariableNames
表的属性来检索变量名称并用元胞数组填充它们。
data= [T.Properties.VariableNames; table2cell(T)];
%or data= [T.Properties.VariableNames; num2cell(table2array(T))];
xlswrite('output',data);
样本运行:
T = table([1;2;3],[NaN; 410; 6],[31; NaN; 27],'VariableNames',{'One' 'Two' 'Three'})
T =
3×3 table
One Two Three
___ ___ _____
1 NaN 31
2 410 NaN
3 6 27
yields:
虽然我认为上面的解决方案更简单,但如果你真的想使用writetable https://www.mathworks.com/help/matlab/ref/writetable.html then:
tmp = table2cell(T); %Converting the table to a cell array
tmp(isnan(T.Variables)) = {[]}; %Replacing the NaN entries with []
T = array2table(tmp,'VariableNames',T.Properties.VariableNames); %Converting back to table
writetable(T,'output.csv'); %Writing to a csv file