File Exchange

image thumbnail

improved xlswrite.m

version (6.12 KB) by Barry Dillon
Makes xlswrite use 'persistent' actxserver and hence much faster


Updated 13 Apr 2010

View License

If writing or reading repeatedly to same spreadsheets very slow to keep creating and deleting the active x server. These extra 4 lines of code cause xlswrite to remember if you accessed this spreadsheet before and then not re-create the active x server.

Cite As

Barry Dillon (2021). improved xlswrite.m (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (29)

Mic Vin

Sorry, I meant "5 lines of code":
So, the 4 lines of code are:

persistent Excel; % assume always the same spreadsheet % 1
persistent priorFile; % need to be sure talking again to same file % 2

and (enclosing the try-catch segment of Excel server loading):
if isempty(Excel) || ~strcmp(priorFile,file) % 3

Excel = (...) % leave it as is according to the file's version
priorFile=file; % note so we know it is same file if coming back again % 4
catch (...)

end % 5

Mic Vin

So, the 4 lines of code are:

persistent Excel; % assume always the same spreadsheet % 1
persistent priorFile; % need to be sure talking again to same file % 2

and (enclosing the try-catch segment of Excel server loading):
if isempty(Excel) || ~strcmp(priorFile,file) % 3

try (...)
catch (...)

end % 4

Mic Vin

I want to transfer these mentioned "4 line of code" to a newer version of xlswrite, i.e. the one used in version 2018b of Matlab (Copyright 1984-2016 The MathWorks, Inc. for the xlswrite.m)
Which exactly are the lines of code?
I see the following:

% keep the excel server alive if possible. B.DIllon

persistent Excel; % assume always the same spreadsheet
persistent priorFile; % need to be sure talking again to same file

But these are actually 2 lines of active code...

Austin Bond

FWIW, I was running MatLab 2017a

Runs smoothly and much faster than the built-in xlswrite function in Matlab. You can rename this function as xlswrite1 to avoid any potential confusion with the built-in xlswrite function in Matlab. Thanks Barry for this wonderful code!

aya saeed

where should I install it to avoid the confusion between it and the built in matlab function ?!

Claudio Gelmi

Thanks! Works great. It reduced in 50% my simulation time.

Peter Scotson

Worked for me (I'm obliged to be using R2014b) because it makes multiple writes to the same workbook practical. Far too slow and usually the original hung if there were a lot of writes to be done.


Barry Dillon

Can't believe it is 6 years since I made this submission!! I'm surprised it is underperforming the normal matlab version but of course they may have improved their version. Against that on 11 July 2016 it seemed to help Pratik. All it does is prevent Matlab creating a new activeXserver every time you write so helpful if you have lots of small writes when you don't want to keep creating new servers. Don't actually know why it would run out of storage faster for Rajesh. Maybe there is some house cleaning happens with all the deletes and recreates as envisaged originally by matlab. Generally lack of deletes has not been a problem and of course faster to not make a new server each time. It must be a different profile of data from Rajesh that does not suit the continuous re-use of the same server. Sorry I can't be more helpful. ps. I still get emails thanking me for this small piece of code. I tell people releasing a piece of open source code involves zero ongoing effort in return for endless gratitude. The opposite of marriage.

Rajesh Rajaram

I used this function to dump large amounts of data into multiple sheets in an excel file. The function did increase the speed (~15% for the overall code), but it eventually seems to be crashing due to lack of storage, while the Matlab's version seems to be chugging well past that crash point. Any ideas?

Pratik Nadkarni

The code drastically reduces the time of xlswrite function. Works wonderfully. Thank you so much . Great work

Pratik Nadkarni

James Ang

Hi, I'm using 2015b.

I downloaded and unzipped the file, placed the 'improved' xlswrite in my current folder together with the m-file I'm working with, disabled the built-in function by removing E:\matlab\toolbox\matlab\iofun from the Matlab path.

I typed
>>which xlswrite -all
and only saw the 'improved' version shown.

I then ran the codes but from the tic toc timer the reading is about 50-ish secs, almost similar to the built-in xlswrite function, also about 50-ish secs.

Did I use the function incorrectly?

Ilya Belevich

Suraj Srivastava


How can I use this function in GUI?

As I have downloaded this file and kept it in my current foldeer. After that I ran the code, but the speed did'nt got improved.

Kindly help me out.


Biagio Ciuffo

I have a model implemented in a spreadsheet and I want to check the results for different combinations of the input parameters. So I made a loop and at each step I use Matlab to write in the excel file the and afterwards to read the value of a specific cell (output of the model). Since I need to do it n thousands of time I was looking for a solution alternative to xlswrite. I tried to modification posted here but I don't see any difference in the performance. I run MATLAB R2014a on a Windows Server 2008 R2 and Excel 2010.
Any suggestion?


I downloaded the zip file, but when I tried to run the code I get an error of not enough input arguments. How do I properly incorporate this improved xlswrite for my use?


how can i download this file please?

David Perry

THANK-YOU x 10! - this code works much faster than the original version (on Win7 - 32b - with 2013a)

Marco Marino

With just 4 lines of code my script has become 4 times faster!

Cheers man!




Oh my god, I can't thank you enough!! I am using MATLAB to locate particles in over 9,000 images and then write location data to excel. This has sped things up incredibly!


Do I need to rename this to xlswrite1.m to distinguish it from the builtin xlswrite?

Sebastian W

I optimized my program before I found this and only got 3 write commands left, but this new xlswrite function is roughly 10 times faster.
Thanks Barry.

Fernando tyffing

Works very good, priori to using this it was impossible to run parfor creating a lot of xls files.

Stan Young

Does this leave anything open in the background that would be of concern if writing a large number of spreadsheets - sort of like leaving file pointers open?


Thank you very much for your effort in going into details of ActiveX server when dealing with xls file using MATLAB.

I have to admit that I have no clue what is going when my xlswrite is running as I know superficially that ActiveX server has to be created and I simply use it. I realise your powerful correction of the xlswrite when I struggle to run 7-8 lines of xlswrite continuosly for 360 times and it kept crashing. I have been trying to run it for 2-3 days and it ends up the same way sooner or later. I have to divide it and run seperately but still a painful delay causes freezing several times.

I totally appreciate on what you have done on this and your contribution is highly regarded as a life and time-saver. I can have my program running and getting results thanks to you.

Thank you very much again and please keep it up.

Kind regards,

MATLAB Release Compatibility
Created with R2009b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!