How to write data from Matlab to Excel (especially when you don’t have Excel)
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)