Code covered by the BSD License  

Highlights from
xlsread.m for faster data transfer

5.0

5.0 | 4 ratings Rate this file 37 Downloads (last 30 days) File Size: 7.24 KB File ID: #22365

xlsread.m for faster data transfer

by Antonio

 

09 Dec 2008 (Updated 08 Jan 2009)

This modified version makes the original xlsread.m faster and also allows add-ins to be loaded.

| Watch this File

File Information
Description

The idea came from a modified version of xlswrite.m (xlswrite1.m) posted by Matt Swartz in this fle exchange area on March 21, 2006. This version of xlsread.m was easily accomplished by removing all instances of server open/close functions within the original file, as it was done with xlswrite1.m. Should one use xlswrite1.m in conjunction with the xlsread.m, the data transfer process still takes too long, in addition to yielding undesirable results due to the persistent server open/close.

In order to use it, one must place the following code within the program, as given by Matt Swartz. It opens/closes the activex server, load an add-in if any (by default when Excel opens as a COM server it does NOT load add-ins), and checks to see if the file already exists:

Excel = actxserver ('Excel.Application');
Excel.Workbooks.Open('C:\YourAddInFolder\AddInNameWithExtension');
Excel.Workbooks.Item('AddInNameWithExtension').RunAutoMacros(1);
File='C:\YourFileFolder\FileName';
if ~exist(File,'file')
    ExcelWorkbook = Excel.Workbooks.Add;
    ExcelWorkbook.SaveAs(File,1);
    ExcelWorkbook.Close(false);
end
Excel.Workbooks.Open(File);
 
Run the xlsread1.m as many times as needed. Then run the following code to close the activex server:

Excel.ActiveWorkbook.Save;
Excel.Quit
Excel.delete
clear Excel

The result was that processing speed of xlsread.m increased dramatically.

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
xlswrite1

MATLAB release MATLAB 7.6 (R2008a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (6)
30 Jun 2009 Oleg Komarov

I would leave in the function:
<pre class = "code">

Excel = actxserver ('Excel.Application');
Excel.Workbooks.Open('C:\YourAddInFolder\AddInNameWithExtension');
Excel.Workbooks.Item('AddInNameWithExtension').RunAutoMacros(1);
File='C:\YourFileFolder\FileName';
if ~exist(File,'file')
    ExcelWorkbook = Excel.Workbooks.Add;
    ExcelWorkbook.SaveAs(File,1);
    ExcelWorkbook.Close(false);
end
Excel.Workbooks.Open(File);
</pre>

Instead when it checks:
if nargin < 1 || isempty(file)
    error('MATLAB:xlsread:FileName','Filename must be specified.');
end

I would substitute the error with:
Excel.ActiveWorkbook.Save;
Excel.Quit
Excel.delete

This way all u have to do is:
xlsread("firstfile"......)
xlsread("secondfile"......)
...and so on until
xlsread() % close teh connection!

That would code saving and clean.

 

28 Jul 2009 lh ‹

>> xlsread1('testsheet.xls')
??? Error using ==> evalin
Undefined function or variable 'Excel'.

15 Oct 2009 Ivo Torre

lh>, I had the same error. My problem was using xlsread1 from within an m function...
The first line of code in xlsread1 is
Excel = evalin('base','Excel');
that tries to look for the object Excel in the base workspace.
But, since I instantiated the excel object in my m function, here's the error we met.
You should modify it in
Excel = evalin('caller','Excel');
that tries to look fo the Excel object in the workspace of the caller, where the object exists.

Hope it works for you

20 Nov 2009 Danila

Thank you, Ivo. Your suggestion helped me - I also use xlsread1 from another function.

17 Jun 2011 Billy

How about we change the first line of code to
Excel = eval('Excel');

22 Jun 2011 Billy

Please ignore my previous comment. This would work!
try
    Excel = evalin('base','Excel');
catch err
    Excel = evalin('caller','Excel');
end

Please login to add a comment or rating.
Updates
08 Jan 2009

Two additional lines were included to allow add-ins to be installed since by default when Excel opens as a COM server it does NOT load add-ins.

Tag Activity for this File
Tag Applied By Date/Time
data export Antonio 10 Dec 2008 15:41:12
data import Antonio 10 Dec 2008 15:41:12
faster xlsread Antonio 10 Dec 2008 15:41:12
data export Cristina McIntire 10 Dec 2008 15:59:40
active x Cristina McIntire 10 Dec 2008 15:59:40
data import Cristina McIntire 10 Dec 2008 15:59:40
addin Antonio 09 Jan 2009 15:18:11
excel Danila 20 Nov 2009 05:52:08
active x Luís 17 Nov 2011 15:08:49

Contact us at files@mathworks.com