If you are using Matlab on a MS-Windows PC with MS-Excel installed, there is no problem reading and writing data to Excel (in case your users/customers only understand this software but you still want to do the computations in Matlab). Here is the code to read (1st line) and write (2nd line):
inputs = xlsread('inputfile.xls', 'inData', 'A1:B3'); [writeStatus, writeMsg] = xlswrite('outputfile.xls', myMatrix, 'outData', 'A1');
Now, there are several reasons why you may not be able to read and write directly to an Excel file: you have Matlab but
- you are on MS-Windows but MS-Excel is not installed (if you use OpenOffice.org, csv files in general or on a server where even Microsoft discourage the use of Excel in back-end, see “More information” here);
- you are on MacOS;
- you are on Linux;
Matlab provides a basic way to read and write in these cases:
xlsread() will only read some xls files (saved as version 97-2003, which should not be an issue – at least this is an Excel file) ;
xlswrite() will only write a .csv file. So you can’t deliver the Excel output.
Fortunately Open Source software is here to help you!
Indeed two recently published code on Matlab Central allow you to write “directly” to Excel.
The first solution is provided by Marin Deresco and apparently uses his own binder to Excel. The most simple code is:
display(['Add Java paths']); javaaddpath('D:\JExcelAPI\jxl.jar'); javaaddpath('D:\JExcelAPI\MXL.jar'); display(['Writing matrix to file']); writeStatus = xlwrite('output.xls', myMatrix, 'outData'); display(['writeStatus: "' num2str(writeStatus) '"']); % 1 = success, 0 = failure
I advise you to put the two .jar files in a central location (as done in the code above). That way all you functions will have access to the same version of these files (and updating them for all your projects will be much easier).
But there are three small issues. First the generated matrix will contain only text fields (and they are formatted as text fields, not numbers) ; it doesn’t really matter because Excel can then use this text as numbers directly. Then the second issue is that although it will actually write the requested data, there will be an error when opening the Excel file if you specified a sheet (‘outData’ here) that already exists. Finally you can only start writing at A1.
The second solution is provided by Alec de Zegher, partly in response to the limitation of the previous code. Alec is taking advantage of the Apache POI java library to handle the writing-side. His code also allows to start writing anywhere in the sheet. And it uses the same call structure:
display(['Add Java paths']); javaaddpath('D:\poi_library\poi-3.8-20120326.jar'); javaaddpath('D:\poi_library\poi-ooxml-3.8-20120326.jar'); javaaddpath('D:\poi_library\poi-ooxml-schemas-3.8-20120326.jar'); javaaddpath('D:\poi_library\xmlbeans-2.3.0.jar'); javaaddpath('D:\poi_library\dom4j-1.6.1.jar'); display(['Writing matrix to file']); writeStatus = xlwrite('output.xls', myMatrix, 'outData', 'B2'); display(['writeStatus: "' num2str(writeStatus) '"']); % 1 = success, 0 = failure
As you can see there are some more .jar to add. On the other hand, it uses a syntax that is very similar to the previous one and to the original Matlab command. Thanks, Marin and Alec, for this great help!
This was tested on Matlab R2012a on a Windows XP 64-bits and with the MCR (same version) on a Windows 2008 64-bits – according to the respective pages for the code, it should work on Mac and other platforms/configurations too.
Photo credits: matlab rubiks cube by gusset, on Flickr (licence by-nc-sa)
3 thoughts on “How to write data from Matlab to Excel (especially when you don’t have Excel)”
I tried your first code above and it came with the following error ” Warning: Could not start Excel server for export.
XLSWRITE will attempt to write file in CSV format.
> In xlswrite (line 174)
writeStatus = 0
writeMsg = message: ‘An error occurred on data export in CSV format.
Error using <a href="matlab:matlab.internal.language…'
A lot of things can go wrong when attempting this (that’s why the 2 other solutions are suggested) …
Are you on a Mac (running MacOS)? It seems OLE communication between Excel and Matlab only works under MS-Windows (see http://www.mathworks.com/matlabcentral/answers/243454-compatibility-issue-running-xlswrite-in-matlab-on-a-mac-computer-error-warning-could-not-start).
If you are on a PC (or at least tunning Windows), is Excel installed? I guess so (but you never know). If so, what are the versions used for Matlab and Excel? From there we could start debugging your error …
Comments are closed.