Need help increasing the speed of my code processing .xlsx files
2 ビュー (過去 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 で Spreadsheets についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!