How to stop Excel processes running in the background, which were started from MATLAB functions xlsread, xlswrite, xlsinfo.

13 Ansichten (letzte 30 Tage)
After using xlsread, a(n) Excel process(es) stay(s) running in the background, even after the function finished executing.
How can I stop the processes from within MATLAB?

Akzeptierte Antwort

MathWorks Support Team
MathWorks Support Team am 29 Jul. 2024
Bearbeitet: MathWorks Support Team am 29 Aug. 2024
In MATLAB R2015b and later, xlsread, xlswrite, xlsfinfo use one Excel process that is opened when one of these functions is opened for the first time and stays open until MATLAB closes.
These functions reuse this process for every call instead of opening and closing Excel every time. This greatly improves the performance of successive calls to xlsread, xlswrite, xlsfinfo.
Solution 1
As of R2019a, the recommendation is not to use xlsread, and instead is to use the readtable function. 
More information about how to transition xlsread code to readtable and similar functions is available here:
Please run the below command in the command window of installed MATLAB R2019a version to get release specific documentation
>> web(fullfile(docroot, 'matlab/ref/xlsread.html'))
>> T = readtable('SampleExcel.xlsx','UseExcel',false);
If you are using a version that supports these functions, it is recommended you use these instead.
Solution 2
Started Excel processes persist stay open in the background, but closing MATLAB should kill the Excel process.
If you want to terminate Excel before closing MATLAB then the following example demonstrates how to close Excel processes started by MATLAB
% Shows a list of active excel processes
!tasklist /FI "IMAGENAME eq excel.exe"
% STOPS ALL EXCEL PROCESSES [USE WITH CAUTION]
system('taskkill /F /IM EXCEL.EXE');
% Starts an Excel Application
excelApp = actxserver('Excel.Application');
% Starts a second Excel Application
excelFile = xlswrite('testfile.xlsx',randn(5));
% Uses the second Excel Instance
xlsread('testfile.xlsx');
% Shows now two running Excel processes
!tasklist /FI "IMAGENAME eq excel.exe"
% Stops the second instance
clear getExcelInstance
!tasklist /FI "IMAGENAME eq excel.exe"
% Stops the first instance
Quit(excelApp)
delete(excelApp)
clear excelApp
% q.e.d.
!tasklist /FI "IMAGENAME eq excel.exe"
Solution 3
Using 'basic' as the fourth argument to xlsread will prevent it from opening Excel instances and use another method instead, but has performance limitations and cannot be used to select ranges of data.
>> [num,txt,raw] = xlsread('SampleExcel.xlsx', '', '', 'basic');
"Basic" flag descibed here:
Limitations described here:
If the Solutions above fail to close the Excel processes spawned by MATLAB or closes those not opened by MATLAB, this solution may apply. 
Such systems include a Windows workstation or server that runs Continuous Integration (CI) tests such as GitLab or another Git CI system.
It should be noted that this is a solution that involves compiling a MATLAB executable C file on the workstation that runs the Test Suite,
so it is not suitable for use in a customer-facing application built with MATLAB because most users do not have C compilers installed on their machines.
Depending on the workflow, calling the
closeExcel
 function at the end of the test suite should be suitable, but it could also be called at the end of each test case if that is found to be more effective.
Example: 
The getProcessId.c file needs to be compiled into a MEX file by executing:
>> mex getProcessId.c
At the end of the Test Suite
>> closeExcel()
when
closeExcel.m and
getProcessId.mex___ are both on the MATLAB path.
The MEX file only needs to be compiled on the machine once, but if there are multiple workstations that run the test suite, it will have to be compiled on each separately.
It is a Windows-specific solution because on other systems MATLAB does not spawn Excel COM objects.
To select the C compiler you would like to use or to get a link to the support package that installs the MINGW C compiler, you can execute:
>> mex -setup
Please follow the below link to search for the required information regarding the current release:

Weitere Antworten (0)

Kategorien

Mehr zu Data Import from MATLAB finden Sie in Help Center und File Exchange

Tags

Noch keine Tags eingegeben.

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by