MATLAB: Does IMPORTDATA take longer than XLSREAD when reading the Excel file in MATLAB 7.7 (R2008b)

20032007lagMATLABmicrosoftslowxlsxlsx

When using IMPORTDATA to read the data in my Excel file (.xls or .xlsx file) into MATLAB, I notice that it can take three or more times longer to execute than a call to XLSREAD. For example:
>> tic; A = xlsread('Data.xlsx'); toc
Elapsed time is 0.778333 seconds.
>> tic; A = importdata('Data.xlsx'); toc
Elapsed time is 3.519739 seconds.

Best Answer

There are a number of reasons why using IMPORTDATA to read your Excel file may be taking significantly longer than XLSREAD.
Firstly, XLSREAD reads data only from one sheet in the Excel file. With the default syntax, XLSREAD will read data from the first sheet. If supplying a sheet name to XLSREAD, data will be read from that specific sheet alone. The primary source of overhead associated with XLSREAD is when it starts up a background automation server instance of Excel.exe before reading the data. It then shuts the Excel instance down at the end of the function.
On the other hand, in the case of Excel files (.xls or .xlsx), IMPORTDATA attempts to read data from each existing sheet in the file. For each existing sheet in the file, it issues a call to XLSREAD, which would start up and shut down an instance of Excel for each call. The add-up of this overhead can be detrimental in the case that you have blank sheets that were not intended to be there (by default, Excel inserts three blank sheets when creating a new file). This may be the case with your Excel file.
In the case that needed data exists in all the sheets in the Excel file, it is more convenient to call IMPORTDATA once than to loop through XLSREAD multiple times.
Also, as IMPORTDATA is designed to handle a wide variety of data files, and there is extra overhead spent in determining the file type in order to call the appropriate import function. If you open the code for the 'importdata.m' MATLAB file by running 'edit importdata' in MATLAB, you can see this aspect of the code up front.
To determine whether the file being read is an Excel file, the XLSFINFO function is called (line 111 of importdata.m), which also starts up a server Excel instance, adding another portion of overhead. On the other hand, XLSREAD does not need to call this function, because it assumes that the file being read is an Excel file to begin with.