Question

I am using xlsread on a 1.4 MB excel file. I started to notice some odd behaviors after running my m code a few times.

  • I cannot open excel files using double click (can only use matlab)
  • 2 LARGE (30Mb) EXCEL.EXE*32 files open per m-code run before a clear (I call the function 2 times)

I fell like matlab is not cleaning up its file handles. My updated code using corner to corner reads reads data using the following two lines

prs = xlsread(file, 'data2','A2:C550');
elm = xlsread(file, 'element','A2:C65536');

Task manager shows two large EXCEL.EXE*32 files after both functions are called. I have tried to

clear
clear all
close all
fclose('all')
fclose(0); fclose(1); fclose(2)

etc. I close matlab and they are still open.

Did some more snooping after trying to restart with no results.

xlsread populates what appears to be a server with excel to read information using

Excel = actxserver('excel.application');

The cleanup looks like it is supposed to happen here

cleanUp = onCleanup(@()xlsCleanup(Excel, file));        
[numericData, textData, rawData, customOutput] = xlsreadCOM(file, sheet, range, Excel, customFun);

followed by a

clear cleanUp; 

later in the program. Research suggests this is supposed to run a clean up function called xlsCleanup. Copied the file here for reference.

function xlsCleanup(Excel, filePath)
    try %#ok<TRYNC> - Suppress any exception
        %Turn off dialog boxes as we close the file and quit Excel.
        Excel.DisplayAlerts = 0; 
        %Explicitly close the file just in case.  The Excel API expects
        %just the filename and not the path.  This is safe because Excel
        %also does not allow opening two files with the same name in
        %different folders at the same time.
        [~, n, e] = fileparts(filePath);
        fileName = [n e];
        Excel.Workbooks.Item(fileName).Close(false);
    end
    Excel.Quit;
end

Firstly, its anoying that it catches exceptions without an alert. I checked, but the code wasn't throwing exceptions. It appears that the line

Excel.Workbooks.Item(fileName).Close(false);

is just not ending the process. I don't know what could be causing this beyond this function (can't step in anymore) and there is no mention on the web about issues with it. Please help me explain this behavior. Taking up all of my memory

Was it helpful?

Solution 3

Still no solution to the matlab issue. This is what I am using to close the million processes that remain open after running my file a few times.

In Cygwin:

ps -W | grep EXCEL | cut -c -9 | xargs /bin/kill -f

OTHER TIPS

Range parameter also works on corners. From the documentation of xlsread:

num = xlsread(filename,sheet,xlRange)

Specify xlRange using the syntax 'C1:C2', where C1 and C2 are two opposing corners that define the region to read. For example, 'D2:H4' represents the 3-by-5 rectangular region between the two corners D2 and H4 on the worksheet. The xlRange input is not case sensitive, and uses Excel A1 reference style (see Excel help).

That means you can just do:

xlsread(file, 'element', 'A2:C65536');

This works for me.

system('taskkill /F /IM EXCEL.EXE');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top