系統版本:Win7 64位
Matlab版本:R2015b
問題描述:使用excel的操作函數,比如xlsread,xlswrite,導致excel進程無法終止,任務管理器中仍殘留excel進程,打開excel文檔,修改后不提示保存。
進程顯示automation -Embedding,應該是某個應用占用了這個進程,然而我並沒有找到是哪個應用。
解決方法:
之前的Matlab R2012a並沒有出現此問題,問題是在R2015b版本中的%Program Files%MATLAB\R2015b\toolbox\matlab\iofun\+matlab\+io\+internal\getExcelInstance.m文件,代碼:
1 function app = getExcelInstance 2 % Copyright 2015 The MathWorks, Inc. 3 4 persistent excelApplication; 5 if isempty(excelApplication) || ~isactive || excelApplication.Visible 6 % If Excel is not available, this will throw an exception. If 7 % Excel has been made visible, we assume the user opened the 8 % worksheet outside MATLAB using the same Excel process, and so we 9 % should start a new process. 10 excelApplication = actxserver('Excel.Application'); 11 end 12 app = excelApplication; 13 14 function tf = isactive 15 % Try accessing a readonly property of the COM server to see if it is 16 % active. 17 try 18 get(excelApplication, 'Version'); 19 tf = true; 20 catch 21 excelApplication.delete; 22 excelApplication = []; 23 tf = false; 24 end 25 end 26 end
第四行的關鍵詞persistent,如果直接換成excelApplication=[],則不會出現進程殘留的情況;
這個文件應該是R2015b版本中加上去的,之前的版本excel操作函數是直接調用actxserver函數,也沒有出現此問題;
個人解決方案:換成R2015a版本,完美運行;
MATLAB各版本的下載地址(可能需要梯子):http://www.eng.utah.edu/downloads/matlab/