Need help increasing the speed of my code processing .xlsx files
조회 수: 1 (최근 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
댓글 수: 0
채택된 답변
Image Analyst
2023년 5월 17일
댓글 수: 2
Image Analyst
2023년 5월 18일
You're welcome. Well that was easy (for me at least). Thanks for accepting! 🙂
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Text Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!