File Exchange

image thumbnail

xlwrite : Export Data to Excel from Matlab on Mac/Win

version (665 KB) by Marin Deresco
xlwrite allows you to export data (2d/3d arrays of double/cell) to Excel from Matlab (Mac/Win)


Updated 03 Aug 2012

View Version History

View License

The function xlwrite has similar syntax and inputs as MatLAB's xlswrite.
It also can write 3-d arrays (xlswrite can't), of cell and double type. To simplify the idea : we forward Matlab data to be exported to a Java function which in turn writes the data to excel.

Note that data to be exported is converted to cell then to java String array.

This workaround is a real working solution, it may need further refinements :
- manage Java heap space, as Java heap memory saturates for large arrays exported many times.
- format dates and strings, as all numbers appear as text in Excel.

Matlab's decimal separator is '.' : in order to be able to work with exported data, users of this solution will have to change Mac preferences regarding the decimal separator (should be ".").
To do so you need to go to System Preferences > International > Formats and click on Customize button in number zone, then type '.' in the field required.
This solution works under Windows.

Test_xlwrite.m contains an example.

Cite As

Marin Deresco (2021). xlwrite : Export Data to Excel from Matlab on Mac/Win (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (23)

Vahid Espanmanesh

For those who still struggle with WriteXL error, just go to Test_xlwrite.m and copy the following lines and paste them write before using "xlwrite" in your main code.


import mymxl.*;
import jxl.*;

Yuer Chang

I still got a problem like this:
"Undefined function or variable 'WriteXL'.

Error in xlwrite (line 108)
WriteXL(java.lang.String(file),Cell2JavaString(data),m(1),m(2),java.lang.String(sheet),exist(file,'file')/2); "

xlwrite line of 108-116 is as below:
if (max(size(m))==2)

error('data Matrix too large, when specifying a single sheet, data must have at most 2 dimensions');

Could anybody teach me how to fix it?

Amin Khalaf

Jason Swenson


Sorry, but it's been 5 years now and there is still nothing better than a "working solution"? I really don't want to have to work myself into running a JAVA script just to print my Matlab output as an XLS file. Why hasn't that been worked into release RS2015a?


Scott Peer

If you are not already using Java with MATLAB, don't waste your time, there are no instructions on how to get this to run.


when i run it and then try to open the xlsx file both under Office 2011 or Office 365 for Mac it tells me that the file is corrupted or invalid. Any suggestions? thanks

Linghao Zhang

Zhigang Xu

Thanks for having this utility for us! It may solve my need: I am writing an Matlab function which uses xlswrite under window OS. The intended user of this function however will be in Mac and Linux OS. I tried your test_xlwrite.m in my window machine and it works. However when I opened the resultant excel file, mat1_excel.xls, I got warning messages for all each of the cells, saying that the numbers are stored as text. I have to manually convert text to number cell by cell. Do you have a better solution to this problem?


Miguel Suastegui

How do I add the POI jar files to the Matlab Java path?

I keep getting this error:
Error using xlwrite (line 93)
The POI library is not loaded in Matlab.
Check that POI jar files are in Matlab Java path!

I'm running MATLAB R2015a on a MacBook air


Keith Johnson

For the people asking about the "WriteXL" error.

In my copy, the indentation was off. I just went to line 107 in the "xlwrite.m" file and corrected it. Hope that works for you as well.

James Russell

The link posted below by Marin Deresco:
will lead you to another version of the same program. This is confusing - there are overlapping programs with overlapping updates. However, the link leads to the latest, and the latest works.

Another confusing thing is managing the Java libraries, which is unexplained. Practically, I have put the poi-library in an area where I keep matlab programs, moved the javaaddpath statements to my startup, which executes from there, and removed them from the test case. The Java paths are relative to wherever javaaddpath is executed (not explained).

It turns out that javaaddpath is inefficient, intended for development only. Instructions on putting a library on your static Java path are in Matlab documentation at Bringing Java Classes into MATLAB Workspace, Static Path. I have not been able to make these work. I have tried various forms of the path, relative and absolute. The instructions seems to be Windows-oriented. If anyone figures out how to make the instructions for static path work, an explanation would be appreciated. Meanwhile the dynamic path does work.

Jolene Atia

I tried this function with Matlab 2015a but it seems to have problems running, any updates? I get Error in xlwrite (line 146)
xlsWorkbook=HSSFWorkbook( );


Like Joachim Seel, but I got a similar error while using xlwrite :
Undefined function 'WriteXL' for input arguments of type 'java.lang.String'.

Error in xlwrite (line 108)
In the folder Archive, there is
Cell2JavaString.m xlwrite.m
MXL.jar cell2char.m
Test_xlwrite.m jxl.jar,
but no WriteXL.m :(
How can I fix this ? I'm a beginner using additional tools like this.


Hi, could I write on specific cells with xlwrite?
If the answer is yes please tell me how!

Joachim Seel

Hi, I have downloaded the xlwrite file but run into this error when trying to run the script below: Any suggestions how to correct this error? Thanks!

Undefined function 'WriteXL' for input arguments of type 'java.lang.String'.

Error in xlwrite (line 68)

This is the script.

%define variables

% read in dynamic load

%pre-allocate for speed

% loop through each row of dynamic_load_matrix and append to
% dynamic_load_vector
for i=1:365









Edward Byers

Complicated to get started with the java files etc and no very good instructions on this. See the example however.
Doesn't support specifying to a range.
However - support multi-dimensional variables which is a great bonus.

Marin Deresco

@ the cyclist:
check the following submission:

Marin Deresco

@ Paul Shoemaker :
Will try to test a quick solution for xlsread soon (1-2 months).


Paul Shoemaker

Very neat! Any chance we might see an xlread variant that is similarly cross-platform?

Marin Deresco

@ the cyclist :
Hope xlwrite will help you. Thank you for the remark about the specific cells export. I didn't focus on this point yet. The idea behind xlwrite was to export Matlab 3d arrays of doubles or cells to excel.
If time permits, my next submission will contain currently missing xlswrite features.

the cyclist

This is an exciting submission, as it is an ongoing frustration that xlswrite doesn't function fully on a Mac.

xlwrite doesn't seem to have the ability to write to specific cells on a worksheet, as xlswrite does. Is that something you are planning to add, or is there some fundamental impediment to doing that? It would be a powerful addition.

MATLAB Release Compatibility
Created with R2010a
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!