Matlab xlsread打开文件并清理
我在一个1.4 MB的excel文件上使用xlsread。 几次运行我的m代码后,我开始注意到一些奇怪的行为。
- 我不能用双击打开excel文件(只能用matlab)
- 2个大文件(30Mb)EXCEL.EXE * 32个文件在每个m代码运行前打开(我调用该函数2次)
我像matlab一样下跌,没有清理它的文件句柄。 我使用angular落转angular读取更新代码读取数据使用以下两行
prs = xlsread(file, 'data2','A2:C550'); elm = xlsread(file, 'element','A2:C65536');
调用这两个函数后,任务pipe理器显示两个大的EXCEL.EXE * 32文件。 我试过
clear clear all close all fclose('all') fclose(0); fclose(1); fclose(2)
等我closuresmatlab,他们仍然是开放的。
尝试重新启动后没有更多的窥探。
xlsread使用excel来填充看起来像excel的服务器
Excel = actxserver('excel.application');
清理看起来应该是在这里发生的
cleanUp = onCleanup(@()xlsCleanup(Excel, file)); [numericData, textData, rawData, customOutput] = xlsreadCOM(file, sheet, range, Excel, customFun);
其次是一个
clear cleanUp;
稍后在程序中。 研究表明这应该运行一个叫做xlsCleanup的清理函数。 复制这里的文件以供参考。
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 = [ne]; Excel.Workbooks.Item(fileName).Close(false); end Excel.Quit; end
首先,它厌恶它没有警惕地捕捉exception。 我检查了,但代码没有抛出exception。 看来,这一行
Excel.Workbooks.Item(fileName).Close(false);
只是没有结束这个过程。 我不知道什么可能导致这个function超越这个function(不能再踏入其中),networking上也没有提到它的问题。 请帮我解释一下这个行为。 占据我所有的记忆
范围参数也适用于angular落。 从xlsread
的文档:
num = xlsread(filename,sheet,xlRange)
使用语法'C1:C2'指定xlRange,其中C1和C2是定义要读取的区域的两个相对的angular。 例如,“D2:H4”表示工作表上的两个angular落D2和H4之间的3乘5的矩形区域。 xlRangeinput不区分大小写,并使用Excel A1参考样式(请参阅Excel帮助)。
这意味着你可以做:
xlsread(file, 'element', 'A2:C65536');
仍然没有解决matlab问题。 这是我用来closures数百个运行我的文件后保持打开的数百个进程。
在Cygwin:
ps -W | grep EXCEL | cut -c -9 | xargs /bin/kill -f
这对我有用。
system('taskkill /F /IM EXCEL.EXE');