Undocumented Matlab
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT

xlswrite for Mac, Linux

August 2, 2012 9 Comments

Numerous Matlab users have expressed frustration over the years at the fact that the xlswrite function, used for saving/updating Excel files, is only supported on the Windows platform. Matlab uses Excel COM to implement xlswrite functionality, and since COM is a Windows-specific technology, xlswrite does not work on non-Windows platforms.
In such cases, Matlab users normally save the data into a CSV (comma-separated values) file, using the built-in csvwrite or dlmwrite functions, or File-Exchange utilities such as Any2CSV. This enables saving the data, but does not enable saving formatting information or graphic objects (e.g., plots or images). As a side note, xlswrite also does not allow saving formatting information or graphic objects, but at least on Windows we can use direct COM to add them (or use my OfficeDoc utility).

Java solutions for spreadsheet access

Luckily, the community of Java developers, which is an order of magnitude larger than the Matlab community, has developed several utilities that we can easily use in Matlab to implement xlswrite‘s functionality on Macs, Linux, and wherever else Matlab may run – even Windows, if we really wish. Most articles on this website that deal with Java focus on its GUI aspects, i.e., how to use Java to improve Matlab’s plain-vanilla appearance/behavior. But Java is in fact a very broad programming platform that has an enormous repository of non-GUI solutions: networking, database support, data structures, computational algorithms, etc. etc. – and a huge number of them are open-source. Unlike Java’s GUI goodies, these Java packages, being non-GUI in nature, are fully supported by Matlab.
The Excel spreadsheets support, and Office support in general, is just another example of the wide range of non-GUI packages available in Java. In fact there are full-fledged Office lookalikes written in Java (most notably the open-source OpenOffice). We can either use these applications directly, or interact with them from Matlab (via Java), just as we can interact with Excel on Windows.
There are several Java packages that enable reading and writing spreadsheet data, without the full-blown Office support. One such open source project is the ODF (Open Document Foundation) Toolkit, which has plenty of online resources. In 2010 a Matlab user contributed a utility to use the ODF Java package for reading and writing ODF-format spreadsheets (*.ods). Other users have also provided ODF utilities for Matlab.
Using ODF is excellent and cross-platform, but does not solve the Excel problem directly, because the ODF format is incompatible with the XLS format. We can use another open-source Java package, JExcelApi, for this. JExcelApi is relatively easy to use and has several online tutorials (example1, example2), although it is not as widely used as ODF.
Another open-source Java package that can be used to directly read and write XLS files is Apache POI. An informal comparison of POI and JExcelApi can be found here.

xlwrite

Very recently, Marin Deresco has posted a Matlab utility called xlwrite that uses JExcelApi for implementing an xlswrite variant that can be used on all Matlab platforms.

After downloading xlwrite, we need to add its two contained JAR files (jxl.jar, MXL.jar) to the Java classpath:

javaaddpath('C:/Yair/Utils/JExcelAPI/jxl.jar')
javaaddpath('C:/Yair/Utils/JExcelAPI/MXL.jar')

javaaddpath('C:/Yair/Utils/JExcelAPI/jxl.jar') javaaddpath('C:/Yair/Utils/JExcelAPI/MXL.jar')

We can now use xlwrite to store data in a real *.xls file:

xlwrite('my_data.xls', magic(3))

xlwrite('my_data.xls', magic(3))

xlwrite has similar syntax and inputs to Matlab’s xlswrite. It can also write 3-d arrays (which xlswrite cannot), of cell and double type (the third dimension is simply stored in separate worksheets).
Note: Matlab’s decimal separator is ‘.’ – in order to be able to work with exported data on Macs, Mac users may need to change Mac decimal separator preferences. To do so you need to go to System Preferences > International > Formats and click on Customize button in the number zone, then type ‘.’ in the required field.
xlwrite is a real working solution. However, it may need further refinements, that will hopefully be added in future updates to this utility:

  • automatically add the javaaddpath commands to xlwrite so that users won’t need to do them
  • manage Java heap space, as Java heap memory saturates for large arrays exported many times (possible memory leak)
  • format dates and strings, as all numbers appear as text in Excel
  • optimize performance (the culprit appears to be a non-vectorized Cell2JavaString function)
  • include formatting and other goodies that the current xlswrite does not

Related posts:

  1. JFreeChart graphs and gauges – JFreeChart is an open-source charting library that can easily be integrated in Matlab...
  2. Fixing Matlab's actxserver – Matlab's COM (ActiveX) server behavior can be fixed in a couple of useful manners. ...
  3. JBoost – Integrating an external Java library in Matlab – This article shows how an external Java library can be integrated in Matlab...
  4. Setting line position in an edit-box uicontrol – Matlab uicontrols have many useful features that are only available via Java. Here's how to access them....
  5. Assessing Java object size in Matlab – Java object sizes are not reported by Matlab, but we can still estimate them using two free external utilities. ...
  6. Using SQLite in Matlab – SQLite databases can be accessed in a variety of different ways in Matlab. ...
ActiveX COM Excel File Exchange Java
Print Print
« Previous
Next »
9 Responses
  1. Frank Midgley August 2, 2012 at 06:49 Reply

    I recently ported some Windows MATLAB code to Linux that creates Excel and Powerpoint files. I found the Apache POI package very helpful. It can create traditional .XLS files or the .XLSX OpenOffice style documents.

    • Yair Altman August 2, 2012 at 07:00 Reply

      @Frank – thanks. You’re right of course – I’ve known POI for several years so I don’t know why I forgot to mention it in the article… It’s now taken its rightful place in the article text.

  2. the cyclist August 2, 2012 at 10:04 Reply

    This is potentially a very exciting submission for Mac users. It has been an ongoing frustration for me that I cannot fully take advantage of xlswrite.

    One caveat, though. Unless I am misunderstanding something, it is not 100% accurate to say that the inputs are the same as those of xlswrite. In particular, it doesn’t seem that xlwrite will accept the “range” argument, that allows the user to specify where on the worksheet to write the array.

    • Yair Altman August 2, 2012 at 11:27 Reply

      @Cyclist – thanks for the catch. I updated the wording accordingly.

    • Marin October 17, 2012 at 08:01 Reply

      @ the cyclist:
      check the following submission:
      http://www.mathworks.com/matlabcentral/fileexchange/38591

      • Yair Altman October 17, 2012 at 11:44

        @Marin – I believe that @cyclist was only trying to say that although your xlwrite utility is great, it does not have the exact same inputs format as xlswrite. If you could make xlwrite use exactly the same inputs format as xlswrite, then it will be much easier for users to use xlwrite.

    • Marin October 26, 2012 at 04:28 Reply

      @Yair – I have to reword my previous comment :
      Alec de Zegher published a utility http://www.mathworks.com/matlabcentral/fileexchange/38591 (also named ‘xlwrite’) and uses exact syntax as xlswrite. It is an excellent work (use of JExcelApi only).
      In contrast, my original submission uses JExcelAPI and MXL package (that I’ve coded to handle 2d/3d data ).

      This is why in my previous comment I pointed out to Alec’s excellent submission : same syntax as in xlswrite. However, Alec’s xlwrite handles 2d data only.
      It seems to me that Mac/Linux/Unix MATLAB users will soon get xlswrite and xlsread full adaptations!
      Have a good day all!

  3. jamy October 7, 2012 at 14:16 Reply

    Hi, Just to follow “The cyclist” post: xlwrite doesn’t accept the “range” argument that allows the user to specify where on the worksheet to write the array. Is there any progress on this?
    Thanks

    Cheers,
    J.

    • Yair Altman October 8, 2012 at 09:06 Reply

      @Jamy – I suggest leaving a comment for Marin Deresco on his xlwrite‘s FEX page.

Leave a Reply
HTML tags such as <b> or <i> are accepted.
Wrap code fragments inside <pre lang="matlab"> tags, like this:
<pre lang="matlab">
a = magic(3);
disp(sum(a))
</pre>
I reserve the right to edit/delete comments (read the site policies).
Not all comments will be answered. You can always email me (altmany at gmail) for private consulting.

Click here to cancel reply.

Useful links
  •  Email Yair Altman
  •  Subscribe to new posts (email)
  •  Subscribe to new posts (feed)
  •  Subscribe to new posts (reader)
  •  Subscribe to comments (feed)
 
Accelerating MATLAB Performance book
Recent Posts

Speeding-up builtin Matlab functions – part 3

Improving graphics interactivity

Interesting Matlab puzzle – analysis

Interesting Matlab puzzle

Undocumented plot marker types

Matlab toolstrip – part 9 (popup figures)

Matlab toolstrip – part 8 (galleries)

Matlab toolstrip – part 7 (selection controls)

Matlab toolstrip – part 6 (complex controls)

Matlab toolstrip – part 5 (icons)

Matlab toolstrip – part 4 (control customization)

Reverting axes controls in figure toolbar

Matlab toolstrip – part 3 (basic customization)

Matlab toolstrip – part 2 (ToolGroup App)

Matlab toolstrip – part 1

Categories
  • Desktop (45)
  • Figure window (59)
  • Guest bloggers (65)
  • GUI (165)
  • Handle graphics (84)
  • Hidden property (42)
  • Icons (15)
  • Java (174)
  • Listeners (22)
  • Memory (16)
  • Mex (13)
  • Presumed future risk (394)
    • High risk of breaking in future versions (100)
    • Low risk of breaking in future versions (160)
    • Medium risk of breaking in future versions (136)
  • Public presentation (6)
  • Semi-documented feature (10)
  • Semi-documented function (35)
  • Stock Matlab function (140)
  • Toolbox (10)
  • UI controls (52)
  • Uncategorized (13)
  • Undocumented feature (217)
  • Undocumented function (37)
Tags
ActiveX (6) AppDesigner (9) Callbacks (31) Compiler (10) Desktop (38) Donn Shull (10) Editor (8) Figure (19) FindJObj (27) GUI (141) GUIDE (8) Handle graphics (78) HG2 (34) Hidden property (51) HTML (26) Icons (9) Internal component (39) Java (178) JavaFrame (20) JIDE (19) JMI (8) Listener (17) Malcolm Lidierth (8) MCOS (11) Memory (13) Menubar (9) Mex (14) Optical illusion (11) Performance (78) Profiler (9) Pure Matlab (187) schema (7) schema.class (8) schema.prop (18) Semi-documented feature (6) Semi-documented function (33) Toolbar (14) Toolstrip (13) uicontrol (37) uifigure (8) UIInspect (12) uitools (20) Undocumented feature (187) Undocumented function (37) Undocumented property (20)
Recent Comments
  • Jianfei (31 days 0 hours ago): I have tried the MathWorks CheckBoxList in Matlab 2015b. For whatever the reason, I can’t change the font properties. I can change the font color, but changing font properties...
  • Donato Coppola (36 days 19 hours ago): Hi Yair, I have a problem with the double format. When I run the treeTable function, the numbers in double format cells are displayed with comma as decimal separator. How can...
  • Kim (42 days 9 hours ago): Yair, the following didn’t work for me either: jh.setBorderPainted(false); Regards, Kim
  • Adil (44 days 11 hours ago): Thank you for the blog, it was useful for me. I have a file named App_project.mlapp.zip and when I extract it through Winzip it gives all the files exactly as you described above. I...
  • Mr Ashley Trowell (46 days 21 hours ago): Thank you so much for this analysis. Also, I find it somewhat horrifying. The take away seems to be to use && / || and NOT and / or. Thanks a bunch! ~Ashley
  • Matt (51 days 22 hours ago): Late reply I know, but you can call custom shortcuts with alt-#. Hold down Alt to see what number is assigned to the shortcuts you’ve created. Now if there was a way to create a...
  • James (59 days 17 hours ago): Is there a way to change the location of the window? They pop up at random locations and the user has to chase them down.
  • Robin (60 days 1 hour ago): Hi Yair, This has been a great help. With the removal of com.mathworks sometime in the future (as announced in R2020b), is there an alternative? For me, just getting a preference...
  • Eric Dziekonski (81 days 23 hours ago): This is fantastic! Thanks everyone! To get access to CEF on 2019b, I had to take the advise of Jeffery Devereux and Xiangrui Li. warning off MATLAB:structOnObject % suppress...
  • Yair Altman (82 days 5 hours ago): jPanel1.setParent(jPanel2) will reparent jPanel1 (and similarly any Java component/container) inside jPanel2 (any Java container). While this might work, it seems to me to be an...
  • Eivind (82 days 20 hours ago): Hi, Thanks for a great function! I was trying to put the main panel of my figure into a scrollable panel with the goal of zooming into the panel using shift+scroll and scrolling up...
  • Yair Altman (97 days 6 hours ago): There is no direct way to replicate the CW in a GUI window, you’ll need to display the text in the GUI using your program logic. Here is one possible implementation that...
  • Sunki Reddy Gunugu (99 days 3 hours ago): hi 1.i would like to know how the error in command window of matlab is captured and displayed in the GUI with the same color in CW 2. Is their any way to replicate the...
  • Matthias Brenneis (101 days 22 hours ago): Hi, thank you so much for the inspiring work and comments! My issue is: As soon as the panel gets an axes, the panel is no longer transparent: f =...
  • Collin (121 days 20 hours ago): Yair, In 2020a Mathworks added this semi-useful class matlab.ui.internal.JavaMigrati onTools Which contains static methods to call javacomponent, actxcontrol or to get an...
Contact us
Undocumented Matlab © 2009 - Yair Altman
Scroll to top