首页 文章

Matlab xlsread打开文件并清理

提问于
浏览
4

我在1.4 MB excel文件上使用xlsread . 运行我的m代码几次后,我开始注意到一些奇怪的行为 .

  • 我无法使用双击打开excel文件(只能使用matlab)

  • 2个大型(30Mb)EXCEL.EXE * 32个文件打开,每个m代码运行前清除(我调用该函数2次)

我像matlab一样没有清理它的文件句柄 . 我使用拐角到拐角读取的更新代码使用以下两行读取数据

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

调用两个函数后,任务管理器显示两个大的EXCEL.EXE * 32文件 . 我试过了

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

我关闭了matlab,它们仍然是开放的 .

在尝试重新启动后没有结果,进行了一些窥探 .

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 = [n e];
        Excel.Workbooks.Item(fileName).Close(false);
    end
    Excel.Quit;
end

首先,它很烦人,它在没有警报的情况下捕获异常 . 我查了一下,但代码没有抛出异常 . 看来就行了

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

只是没有结束这个过程 . 我不知道是什么导致这个功能超出这个功能(不能再介入了),并且网上没有提及它的问题 . 请帮我解释一下这种行为 . 占据我所有的记忆

3 回答

  • 1

    范围参数也适用于角落 . 从xlsread的文档:

    num = xlsread(filename,sheet,xlRange)
    

    使用语法'C1:C2'指定xlRange,其中C1和C2是定义要读取的区域的两个相对的角 . 例如,'D2:H4'表示工作表上的两个角D2和H4之间的3×5矩形区域 . xlRange输入不区分大小写,并使用Excel A1参考样式(请参阅Excel帮助) .

    这意味着你可以这样做:

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

    这适合我 .

    system('taskkill /F /IM EXCEL.EXE');
    
  • 0

    仍然没有matlab问题的解决方案 . 这就是我用来关闭运行我的文件几次后仍然打开的百万个进程 .

    在Cygwin:

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

相关问题