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

1 Ansicht (letzte 30 Tage)
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

Akzeptierte Antwort

Image Analyst
Image Analyst am 17 Mai 2023
I'd try using readmatrix or readcell instead of xlsread and see if that helps.
  2 Kommentare
Kyle
Kyle am 18 Mai 2023
Thank you, you have saved me so much time.
Image Analyst
Image Analyst am 18 Mai 2023
You're welcome. Well that was easy (for me at least). Thanks for accepting! 🙂

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by