Tag: programming

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;
  • etc.

matlab rubiks cubeMatlab 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)

A good issue of Nature, obviously!

The October 14th, 2010 issue of Nature is obviously a good one. It had to be a good one! I usually advocate Open Access but it is always nice to reading complimentary issues of Nature which is Closed Access but is also publishing very good articles about science at the same time.

In this issue, I was interested in various topics …

First, there is a serie of articles about the US midterm elections and what (US) scientists feel about two years of Obama administration. Obama promised total transparency in American science, a new era of integrity and more freedom for scientists. From what I read, this isn’t the case yet.

Then, there are two article about publishing computer code from scientific experiment. In a World View, Nick Barnes, director of the Climate Code Foundation takes some concerns about that to pieces. The main reasons to provide computer code is to improve programming skills (the software author’s and others’) and enable others to engage with your research. Don’t be ashamed of the quality of your code. Don’t be afraid of starting the trend if no one around you share their software. Don’t be afraid to refuse support when badly asked for. Don’t overestimate the intellectual property value of your code. Nick Barnes also wrote a blog post about it. And you can comment on the Nature article here.

In a News Feature, Zeeya Merali tells stories of scientists who found themselves in uneasy positions regarding to the software they wrote but, at the same time or later on, realised publishing their software was the best thing to do. Besides formalising one’s training in programming, Zeeya Merali advocates some simple steps to practise “safe software”: use a version-control system ; track sources, data and events ; write testable software ; test the software and encourage sharing of software.

I was also interested in these articles in two ways. First, I also realised the need of a formal training in programming during my Ph.D. and I eventually got a B.Sc. in computer science. In the team and field where I’m now working (Health Economics and modelling of infectious disease), I can clearly see the benefit of having such training. Don’t get me wrong: I meet wonderful colleagues every day who don’t have the training but have lots of skills and can solve lots of problems ; I just often see the ease to grasp algorithms as well as some better procedures and testing that comes with training. And, second, I would like to addd that free software licences are to be considered when publishing your software (for science or not).

In the same issue, there was also a small news about the Europe’s use of research animal. This reminded me of the good-old-days :-). Fundamental biology still account the largest proportion of animal experiments but what intrigued me is the 12.2% of “Other” usage.

There was also a comment on a book, The Professional Guinea Pig, about paid participants in phase I clinical trials. Interesting perspective from “the other side” of trials.

There is also an Outlook on the Lindau Nobel Laureate Meeting. It features interviews with 10 Nobel laureates (it’s always nice to readd their comments on science) and an article about science in the digital age (not yet read but it seems interesting).

Finally, an ad for my previous lab (GIGA ULg) caught my attention, as well as the small article about Science in Belgium. I must however write that this article is a bad summary of the research landscape in Belgium with, for example, mention of only two universities: the KUL and the UCL (we have many more). Souvenirs, souvenirs.

Some useful software for programmers stuck on MS-Windows

Sometimes, even if you mainly develop on Unix/Linux boxes, you are stuck with MS-Windows on your desktop. Moreover, although your are a developer (i.e. someone who is supposed to know how to run a computer), you have no administrator rights so you can’t install the right tools that can improve your productivity and enhance your code stability/security. This is for the sad part.

Fortunately, Free Software are there and most of them can even be run without being installed on your machine, just copy the software and use it! Here is a list of some of the software I’m using. Feel free to promote your favourite application in the comments.

  • CamStudio: record video of bugs for support (free to use)
  • Virtual Desktop Manager: many desktops on MS-Windows (free to use)
  • doxygen: automatically generate documentation from your code (Free Software)
  • Eclipse: integrated development environment with support of many languages (including old COBOL!) and version control systems (Free Software)
  • FileZillaPortable: move file to/from servers (Free Software)
  • Firefox: test web application in a popular browser (Free Software)
  • freemind: mind mapping application (Free Software)
  • GIMPPortable: edit screenshots when you encounter a bug (Free Software)
  • htmlhelp: edit help files for end-users (free to use)
  • InkscapePortable: easily draw any scheme (Free Software)
  • j2sdk1.4: support old Java applications (free to use)
  • jdk1.6: develop and run new Java applications (free to use)
  • LaTeX (MiKTeX): write your dev reports (Free Software)
  • Lynx: surf fast without distraction (Free Software)
  • Netbeans: integrated development environment with support of version control systems (Free Software)
  • PuTTY: connect to servers in SSH (Free Software)
  • PuTTY connection manager: regroup PuTTY windows (Free Software)
  • SumatraPDF: free PDF reader (Free Software)
  • TeXnicCenter: LaTeX editor working with MiKTeX (Free Software)
  • tidy.exe: tidy your HTML code (Free Software)
  • todolist: simple todolist (Free Software)
  • TOra: Oracle database front-end (Free Software)
  • VLCPortable: listen to music and watch all movies (Free Software)
  • wget.exe: easily get files without a browser (Free Software)
  • xmlstarlet: check and process XML files (Free Software)