matlab actxserver

时间:2011-12-05 23:34:29

标签: excel matlab

以下循环使用actxserver将数据从excel读入matlab。循环不会由于错误而完成,该错误表明来自excel的数据太大。我发现这很难相信,因为有75个excel表,每个表有大约14000行和21列,所以数据集不是非常庞大。因此,我不确定导致问题的循环或数据集是否太大。

脚本如下:

clear all
%obtain the name of each of the folders under investigation
path='F:\University\CEH Lancaster\Project\Practice'; 
folder = path; 
dirListing = dir(folder); 
dirListing=dirListing(3:end);%first 2 are just pointers
for i=1:length(dirListing);
    Folder_Name{i}=dirListing(i,1).name;
    f{i} = fullfile(path, dirListing(i,1).name);%obtain the name of each folder
    files{i}=dir(fullfile(f{i},'*.xls'));%find the .xls files
    for j=1:length(files{1,i});
        File_Name{1,i}{j,1}=files{1,i}(j,1).name;%find the name of each .xls file in each folder
    end
end
%manually obtain the name of the required worksheets
Name_workbook={'Data1', 'Data2', 'Data3', 'Data4'};
%read data in from excel
excel = actxserver('Excel.Application');
excel.Visible=0;
for i=1:length(File_Name);
    a(i)=length(File_Name{1,i});
    for j=1:a(i);
        file{1,i}{j,1}=excel.Workbooks.Open(fullfile(path,Folder_Name{1,i},File_Name{1,i}{j,1}));
        sheet1{1,i}{j,1}=excel.Worksheets.get('Item', Name_workbook(1,i));
        MyRange{1,i}{j,1}=sheet1{1,i}{j,1}.UsedRange; 
        MyData{1,i}{j,1}=MyRange{1,i}{j,1}.Value;        
    end
end

有没有人对我接下来要尝试的内容有所了解?

1 个答案:

答案 0 :(得分:1)

您在阅读数据后是否要关闭工作簿?如果您正在打开文件而不关闭它们,那么excel可能会同时打开太多工作簿。