Need help increasing the speed of my code processing .xlsx files

3 次查看(过去 30 天)
Hello, I am trying to increase the speed of my code. This project is meant to read through a folder of xlsx files with over 6000 files. Right now, it takes about an hour to go through 4000. I cannot share the files it reads, but they are xlsx files with names and dates at the top (which is what I’m an looking for and writing into the table) but they also contain 3-4 pictures in each file (that really increase the file size). I do not care about the photos but did not know if that contributes to the slow speed of this code.
If anyone knows a way to increase the speed with keeping all the same functions, that would be much appreciated.
%% Problem 1
o = 1;
prompt = " (1) or (2): ";
Answer = input(prompt);
s1= ["Graphics"; "Pictures"; "Sheet"]; % name of sheets that you want the code to skip over so it can avoid errors.
Errors = 0;
if (Answer == 1)
fds = fileDatastore('*.xlsx', 'ReadFcn', @importdata);
fullFileNames = fds.Files;
numFiles = length(fullFileNames);
o = 1;
for k = 1 : numFiles
fprintf('Now reading file %s\n', fullFileNames{k});
[status,sheets] = xlsfinfo(fullFileNames{k});
sheet = string(sheets);
S = length(sheets);
i=1;
for x = i:S
p = sheet(1,i);
s2 = p;
tf = strncmpi(s1,s2,2);
tfs = sum(tf);
if tfs == 0
[~,~,dat]=xlsread(fullFileNames{k},p);
dat=[dat(3,1) dat(3,2) dat(3,3) dat(3,4)];
i= i+1;
TableOfValues(o,:) = dat;
o = o +1;
else
end
end
end
elseif (Answer == 2)
fds = fileDatastore('*.xls', 'ReadFcn', @importdata);
fullFileNames = fds.Files;
numFiles = length(fullFileNames);
o = 1;
% Loop over all files reading them in and plotting them.
for k = 1 : numFiles
fprintf('Now reading file %s\n', fullFileNames{k});
[status,sheets] = xlsfinfo(fullFileNames{k});
sheet = string(sheets);
S = length(sheets);
i=1;
for x = i:S
try
p = sheet(1,i);
[~,~,dat]=xlsread(fullFileNames{k},p);
dat=[dat(3,4) dat(3,10) dat(4,4)];
i= i+1;
TableOfValues(o,:) = dat;
o = o +1;
catch
Text = ['Error In Previous File Format', p];
disp(Text);
Errors = Errors + 1;
end
end
end
else
disp("Error")
end

采纳的回答

Image Analyst
Image Analyst 2023-5-17
I'd try using readmatrix or readcell instead of xlsread and see if that helps.

更多回答(0 个)

产品


版本

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by