MATLAB: This code is taking a lot of time to run,

data import

Is there anyway i can write this code efficiently and reduce runtime? I am trying to import data from multiple files in a folder
clc;clear;
testfiledir = 'C:\Users\bidgu\OneDrive\Desktop\manpower';
matfiles = dir(fullfile(testfiledir, '*.xlsm'));
nfiles = length(matfiles);
datafi = cell(nfiles);
for i = 1 :nfiles
% fid = fopen( fullfile(testfiledir, matfiles(i).name) );
datafi{i} = readtable(fullfile(testfiledir, matfiles(i).name));
% fclose(fid);
end

Best Answer

If all the files have the same format, I have seen big improvements using detectImportOptions() on the first file re-use those options for all subsequent ones (expecially if the excel files are formatted oddly):
This is a cumbersome solution, but if you really need speed and will re-use a lot, may be worth it to interact directly with the Excel com server object, which in my experience is much faster to read and write data (at least within a single file).