Matlab使用xlsread, xlswrite函数导致excel进程无法终止的问题

时间:2023-03-08 20:28:30

系统版本:Win7 64位

Matlab版本:R2015b

问题描述:使用excel的操作函数,比如xlsread,xlswrite,导致excel进程无法终止,任务管理器中仍残留excel进程,打开excel文档,修改后不提示保存。

Matlab使用xlsread, xlswrite函数导致excel进程无法终止的问题

参考:http://www.mathworks.com/matlabcentral/answers/98261-why-do-some-excel-processes-fail-to-terminate-after-using-xlsread-xlswrite-or-xlsfinfo-in-matlab

进程显示automation -Embedding,应该是某个应用占用了这个进程,然而我并没有找到是哪个应用。

解决方法:

之前的Matlab R2012a并没有出现此问题,问题是在R2015b版本中的%Program Files%MATLAB\R2015b\toolbox\matlab\iofun\+matlab\+io\+internal\getExcelInstance.m文件,代码:

 function app = getExcelInstance
% Copyright The MathWorks, Inc. persistent excelApplication;
if isempty(excelApplication) || ~isactive || excelApplication.Visible
% If Excel is not available, this will throw an exception. If
% Excel has been made visible, we assume the user opened the
% worksheet outside MATLAB using the same Excel process, and so we
% should start a new process.
excelApplication = actxserver('Excel.Application');
end
app = excelApplication; function tf = isactive
% Try accessing a readonly property of the COM server to see if it is
% active.
try
get(excelApplication, 'Version');
tf = true;
catch
excelApplication.delete;
excelApplication = [];
tf = false;
end
end
end

第四行的关键词persistent,如果直接换成excelApplication=[],则不会出现进程残留的情况;

这个文件应该是R2015b版本中加上去的,之前的版本excel操作函数是直接调用actxserver函数,也没有出现此问题;

个人解决方案:换成R2015a版本,完美运行;

MATLAB各版本的下载地址(可能需要*):http://www.eng.utah.edu/downloads/matlab/