矢量化-将excel文件导入matlab
我编写了以下函数,用于将excel文件导入matlab。该函数工作正常,通过插入文件的路径名,脚本将文件导入工作区。功能如下所示:矢量化-将excel文件导入matlab,matlab,for-loop,vectorization,Matlab,For Loop,Vectorization,我编写了以下函数,用于将excel文件导入matlab。该函数工作正常,通过插入文件的路径名,脚本将文件导入工作区。功能如下所示: function Data = xls_function(pathName); %Script imports the relevant .xls files into matlab - ensure that the .xls %files are stored in a folder specified by 'pathName'. %------------
function Data = xls_function(pathName);
%Script imports the relevant .xls files into matlab - ensure that the .xls
%files are stored in a folder specified by 'pathName'.
%--------------------------------------------------------------------------
TopFolder = pathName;
dirListing = dir(TopFolder);%Lists the folders in the directory specified
%by pathName.
dirListing = dirListing(3:end);%Remove the first two structures as they
%are only pointers.
for i = 1:length(dirListing);
SubFolder{i} = dirListing(i,1).name;%obtain the name of each folder in
%the specified path.
SubFolderPath{i} = fullfile(pathName, dirListing(i,1).name);%obtain
%the path name for each of the folders.
ExcelFile{i} = dir(fullfile(SubFolderPath{i},'*.xls'));%find the
%number of .xls files in each of the SubFolders.
for j = 1:length(ExcelFile{1,i});
ExcelFileName{1,i}{j,1} = ExcelFile{1,i}(j,1).name;%find the name
%of each .xls file in each of the SubFolders.
for k = 1:length(ExcelFileName);
for m = 1:length(ExcelFileName{1,k});
[status{1,k}{m,1},sheets{1,k}{m,1},format{1,k}{m,1}]...
= xlsfinfo((fullfile(pathName,SubFolder{1,k},...
ExcelFileName{1,k}{m,1})));%gather information on the
%.xls files i.e. worksheet names.
Name_worksheet{1,k}{m,1} = sheets{1,k}{m,1}{1,end};%obtain
%the name of each of the .xls worksheets within
%each spreadsheet.
end
end
end
end
for n = 1:length(ExcelFileName);
for o = 1:length(ExcelFileName{1,n});
%require two loops as the number of excel spreadsheets varies
%from the number of worksheets in each spreadsheet.
TXT{1,n}{o,1} = xlsread(fullfile(pathName,SubFolder{1,n},...
ExcelFileName{1,n}{o,1}),Name_worksheet{1,n}{o,1});%import the
%relevant data from excel by using the spreadsheet and
%worksheet names previously obtained.
Data.(SubFolder{n}){o,1} = TXT{1,n}{o,1};
end
end
该脚本唯一的问题是,如果.xls文件的数量很大,则运行该脚本所需的时间太长。我已经读到矢量化可以提高运行时间,因此我想知道如何通过矢量化来修改代码以加快运行速度
我意识到阅读这样的代码并不容易(特别是我的编码方式并没有我所希望的那么有效),但如果能提供任何建议,我将不胜感激 我认为矢量化不适用于你的问题,而是一个接一个地应用于你的问题 作为数据示例,您可以使用cellfun将循环矢量化:
tmp = ExcelFileName{1,n}
result_cell = cellfun(@(x) xlsread(fullfile(pathName,x)),tmp, 'UniformOutput', false))
但关键问题是
xlsread
和其他excel相关函数在matlab中的实现较差。他们所做的是使用每个(!)函数调用创建一个新的excel进程(隐藏),在该进程中执行您的命令,然后结束它
我记得当时有一个工具重用了同一个excel实例,因此速度非常快——但不幸的是,我再也找不到它了。但也许你可以在那里找到一个例子,你可以基于这个例子,你自己的读者可以重用它
另一个相关的注意事项是,Excel有一个愚蠢的限制,即它不允许同时打开两个同名的文件,然后由于一些错误而失败。因此,如果您运行矢量化/并行阅读,您将获得一个全新的奇怪错误乐趣:D
就我自己而言,我找到了通过java处理这些文档的唯一方法。这些都有一个很好的优点,你不需要安装Excel,但不幸的是需要一些编程。我不认为矢量化适用于你的问题,而是一个接一个 作为数据示例,您可以使用cellfun将循环矢量化:
tmp = ExcelFileName{1,n}
result_cell = cellfun(@(x) xlsread(fullfile(pathName,x)),tmp, 'UniformOutput', false))
但关键问题是
xlsread
和其他excel相关函数在matlab中的实现较差。他们所做的是使用每个(!)函数调用创建一个新的excel进程(隐藏),在该进程中执行您的命令,然后结束它
我记得当时有一个工具重用了同一个excel实例,因此速度非常快——但不幸的是,我再也找不到它了。但也许你可以在那里找到一个例子,你可以基于这个例子,你自己的读者可以重用它
另一个相关的注意事项是,Excel有一个愚蠢的限制,即它不允许同时打开两个同名的文件,然后由于一些错误而失败。因此,如果您运行矢量化/并行阅读,您将获得一个全新的奇怪错误乐趣:D 就我自己而言,我找到了通过java处理这些文档的唯一方法。这些都有一个很好的优点,你不需要安装Excel,但不幸的是需要一些编程