这是 R2015b 左右引入的一个“功能”,用于加速对 Excel 的多次写入...对用户/内存不太友好!
The xlswrite
文档链接到此MathWorks 支持解答使用手动写入 Excelactxserver
,然后您可以手动删除引用 Excel 的 COM 对象。
你其实可以edit xlswrite
并看到它使用matlab.io.internal.getExcelInstance
,它执行相同的操作并创建一个 COM 接口actxserver
.
一个厚脸皮的选择是复制xlswrite
,添加Excel
它创建作为输出的变量,并且Quit
and delete
之后就可以了,如下图所示。我不主张破坏 MathWorks 对该函数的任何版权所有权。
一个不那么厚颜无耻的选择是根据我上面链接的答案创建一个类似的函数,仅用于写入数据,它看起来像这样:
function xlswriteClean( File, Data, Range )
% XLSWRITECELAN writes data like XLSWRITE, but deletes the Excel instance!
% XLSWRITECELAN (FILE,DATA,RANGE) writes the variable in
% DATA to FILE, in the range specified by RANGE.
% RANGE is optional, defaults to "A1"
% Obtain the full path name of the file
% Could handle this more elegantly, i.e.
% this always assumes the current directory, but user might give a full path
file = fullfile(pwd, File);
% Open an ActiveX connection to Excel
h = actxserver('excel.application');
%Create a new work book (excel file)
wb = h.WorkBooks.Add();
% Select the appropriate range
if nargin < 3
Range = 'A1';
end
rng = h.Activesheet.get('Range', Range);
% Write the data to the range
rng.value = Data;
% Save the file with the given file name, close Excel
wb.SaveAs( File );
% Clean up - the point of this function
wb.Close;
h.Quit;
h.delete;
end
您可以使用 COM 对象自定义新 Excel 工作簿中的基本所有内容h
,这样您就可以添加您使用的任何功能xlswrite
比如工作表命名等。