MATLAB Answers

Why do some Excel processes fail to terminate after using XLSREAD, XLSWRITE or XLSFINFO in MATLAB?

236 views (last 30 days)
When I use a command that calls Excel as an ActiveX server, such as XLSREAD, XLSWRITE, or XLSFINFO, an Excel.exe process is left in memory even after the command is finished or I close MATLAB. If I am running a script with many calls to XLSREAD, the ghost processes fill up my memory.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 10 Dec 2018
Edited: MathWorks Support Team on 10 Dec 2018
As of R2015b, this issue has been resolved as MATLAB will now only open one instance of Excel for a file and does not attempt to close it after the call.
Previous to R2015b, in some circumstances Excel may not shut down after calling XLSREAD, XLSWRITE, or XLSINFO from MATLAB.
Whenever one of these commands is executed in MATLAB on Windows and Excel is installed, a new instance of Excel is started to perform the operation using Excel's ActiveX interface. After the operation is completed, MATLAB requests that Excel quit, however in some circumstances, Excel may ignore this request.
One reason this may occur is if some other application has established an ActiveX connection to Excel and is hanging on to a reference to the interface, and not releasing it. The way ActiveX works is that if 2 applications both make connections to the same server, the server will not close until both applications have terminated their connection. The connection from MATLAB is being terminated, however it is possible that another application has made a connection to Excel. In the past we noticed an issue where Google Desktop Search would create such a connection, however starting with MATLAB 7.0.4 (R14SP2) this issue was resolved by working around the specific way that Google Desktop Search made its connection. If you are experiencing this same issue with a later version of MATLAB, or do not have Google Desktop Installed, there is likely another application on your computer which is holding on to the Excel application ActiveX reference.
One way to work around this issue is to identify the application that is causing this issue, then disable it. You can also contact the company which created the software to notify them of this issue.
Certain Excel add-ins may also prevent proper termination of Excel. Some known add-ins that might cause issues are Broadcom's "Send to Bluetooth" add-in (btsendto_office.dll) and Bloomberg's TaskpaneHost add-in. Disabling the add-ins that cause issues is a possible workaround.
When you use a Bloomberg add-in, that causes this issues: Please open a help chat (by pressing the help key twice on the Bloomberg keyboard) and ask for the technical departement. Bloomberg will then have a look into these issues.
As a last resort you can use the SYSTEM command via MATLAB to kill the process.
system('taskkill /F /IM EXCEL.EXE');

  7 Comments

Show 4 older comments
Jan
Jan on 30 Nov 2017
Moved from a flag of lux :
it's very helpful, tks
@lux: Please use flags only to inform the admins and editors about contributions, which might collide with the terms of use e.g. by rudeness. Thanks.
Matt
Matt on 11 Dec 2017
Matlab 2016a this is happening with Excel Professional 2013. In an odd twist all other Excel instances opened are locked into not warning when exiting with changes. Killing the tasks is not good because there could be unrelated Excel instance that will close without prompting to save changes...losing work. Closing all instances of Excel and then reopening reverts Excel back to normal, prompting saves when exiting with changes.
Dima
Dima on 27 Feb 2019
This is not a working solution - this code terminates all instances of Excel -we are runnign loop with excel link - we need the master excel life to stay active - can this be done? keep the first looping excel process active and kill all excels opened by Matlab??

Sign in to comment.

More Answers (6)

Daniel Atanasov
Daniel Atanasov on 14 Sep 2018
Hey guys, I had to solve this problem myself somehow without killing all open Excel processes but only the ghost ones. I wrote this code that just gets the IDs of all Excel processes before and after xlsread or xlswrite with strfind() and deletes the new ghost ones with taskkill. Sorry for the bad code, but I hope it helps.
[~,tasks] = system('tasklist/fi "imagename eq Excel.exe"');
tasks=sscanf(tasks,'%s');
exe=strfind(tasks,'.EXE');
console=strfind(tasks,'Console');
pid_before=zeros(1,size(exe,2));
for i=1:1:size(exe,2)
pid_before(1,i)=convertCharsToStrings(tasks((exe(i)+4):(console(i)-1)));
end
%Your xlsread/xlswrite code
[~,tasks] = system('tasklist/fi "imagename eq Excel.exe"');
tasks=sscanf(tasks,'%s');
exe=strfind(tasks,'.EXE');
console=strfind(tasks,'Console');
pid_after=zeros(1,size(exe,2));
for i=1:1:size(exe,2)
pid_after(1,i)=convertCharsToStrings(tasks((exe(i)+4):(console(i)-1)));
end
if size(pid_after,2)>size(pid_before,2)
command=['taskkill /f /PID ',mat2str(pid_after(1,end))];
system(command)
end

  13 Comments

Dima
Dima on 1 Mar 2019
I tested this scrip (I am using Matlab R2011b):
[~,tasks] = system('tasklist/fi "imagename eq Excel.exe"');
tasks=sscanf(tasks,'%s');
exe=strfind(tasks,'.EXE');
console=strfind(tasks,'Console');
pid_before=zeros(1,size(exe,2));
for i=1:1:size(exe,2)
pid_before(1,i)=str2doubles(tasks((exe(i)+4):(console(i)-1)));
end
%Your xlsread/xlswrite code
[~,tasks] = system('tasklist/fi "imagename eq Excel.exe"');
tasks=sscanf(tasks,'%s');
exe=strfind(tasks,'.EXE');
console=strfind(tasks,'Console');
pid_after=zeros(1,size(exe,2));
for i=1:1:size(exe,2)
pid_after(1,i)=str2doubles(tasks((exe(i)+4):(console(i)-1)));
end
if size(pid_after,2)>size(pid_before,2)
command=['taskkill /f /PID ',mat2str(pid_after(1,end))];
system(command)
end
and got this error:
Undefined function 'str2doubles' for input arguments of type 'char'.
Error in indicatorsOUTblack2 (line 10)
pid_before(1,i)=str2doubles(tasks((exe(i)+4):(console(i)-1)));
Dima
Dima on 1 Mar 2019
greta - this seems to work:
[~,tasks] = system('tasklist/fi "imagename eq Excel.exe"');
tasks=sscanf(tasks,'%s');
exe=strfind(tasks,'.EXE');
console=strfind(tasks,'Console');
pid_before=zeros(1,size(exe,2));
for i=1:1:size(exe,2)
pid_before(1,i)=str2double(tasks((exe(i)+4):(console(i)-1)));
end
[Num,Txt,Raw]=xlsread('test-BB.xls'); %'test','A1:FA2000','basic');
[~,tasks] = system('tasklist/fi "imagename eq Excel.exe"');
tasks=sscanf(tasks,'%s');
exe=strfind(tasks,'.EXE');
console=strfind(tasks,'Console');
pid_after=zeros(1,size(exe,2));
for i=1:1:size(exe,2)
pid_after(1,i)=str2double(tasks((exe(i)+4):(console(i)-1)));
end
if size(pid_after,2)>size(pid_before,2)
command=['taskkill /f /PID ',mat2str(pid_after(1,end))];
system(command)
end
I need to ask one thing - after the above code I have a lot of lines of code - does closing the excel - after it has been used to read in the data - in any way affect the operation of the remining code? I guess the data is loaded into matlab and then excel can be killed safely?

Sign in to comment.


ybshokouhi
ybshokouhi on 24 Aug 2017
I think this post requires an update. 'taskkill' is not recognized as an internal or external command, operable program or batch file.

  10 Comments

Show 7 older comments

Sign in to comment.


Scott
Scott on 16 Aug 2019
I'll tell you why I am hell bent to so speak on killing the Excel processes--because every time I read in Excel in MATLAB, then open some other Excel spreadsheet outside of MATLAB, if I make changes in the other Excel spreadsheet, and I forget to save that other Excel spreadsheet, Excel does not ask me if I want to save changes, and thus the modifications are LOST. I've lost tons of Excel file mods because I can't stop the Excel process that MATLAB opens. All I need is a simple command to shut down the Excel process that MATLAB opens once I am done reading in the data. I am tired of losing data.

  1 Comment

Guillaume
Guillaume on 16 Aug 2019
Which version of excel would that be?
For me, the excel process that matlab opens is a completely separate process than the one that gets created if I start excel outside matlab. So, I end up with two processes, the interactive excel, and the matlab one. Note that this is something that matlab has no control over. This is solely controlled by excel. The two processes being independent, what matlab does with its own process cannot have any effect on the interactive one.
Excel does not ask me if I want to save changes
This will happen if you forcefully terminate the excel process (e.g. with taskkill), since forcefully terminating a process will not let it do its normal shutdown tasks. Hence, if you leave the processes alone, this should not happen.

Sign in to comment.



Charper
Charper on 27 Aug 2019
I want to chime in and add some useful information since this appeared on Google and helped me solve my problem.
We're using MATLAB + Jenkins in a Windows evnvironment and found that MATLAB leaves "zombie" Excel processes hanging around. After building up for a few days, this caused all kinds of weird effects, ranging from "insufficient memory resources" in Java and other processes despite having tons of free RAM, MATLAB just 'stalling' without doing anything, and even failing system() calls.
Solution - we ran the following batch commands both before and after the Jenkins command-line Matlab operation:
taskkill /F /IM EXCEL.EXE
EXIT /B 0
The 'taskkill' was as suggested here. However, it returns a positive error code. The 'exit' command returns a zero from the batch command so that Jenkins does not mark the build as failed if no Excel processes are stuck.

  0 Comments

Sign in to comment.


Scott
Scott on 27 Aug 2019
I don't know what "Jenkins" is...but "taskkill" is not a valid MATLAB function in the basic license. However, !taskkill /F /IM EXCEL.EXE does work. Just put an exclamation point in front, the command is recognized, and the Microsoft Excel process gets dumped in the Task Manager.

  0 Comments

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!