Groovy programming in MATLAB

Groovy ( http://groovy.codehaus.org/ ) is a modern JVM-based programming language that can be used easily within MATLAB if you add groovy-all.jar to the MATLAB javaclasspath.
As a Python-like, dynamically-typed language, it’s likely to be attractive to the some MATLAB users. http://groovy.codehaus.org/Cookbook+Examples has examples of using Groovy with databases, XML, URLs and more.
Groovy “closures” can be assigned to MATLAB variables and work much as MATLAB anonymous functions. You can also explore Groovy on-the-fly by invoking a groovy console session in MATLAB (works on Windows and from R2012a on Mac/Linux):
>> console=groovy.ui.Console();
>> console.run();
A search for “groovy” on Answers and the Newsgroup gives no hits – hence this “question” to draw more attention to it.

7 Comments

Image Analyst
Image Analyst on 1 Jul 2012
Edited: Image Analyst on 1 Jul 2012
Do you have a simple example of something I can do with Groovy that can't be done with MATLAB? Or that can be done much much easier? I would need a reason to spend time learning another language.
@ImageAnalyst
The point of the post was to advertise its availability to the MATLAB user rather than to evangelize on its merits but, anything a user might presently use Java for from inside MATLAB can be done also in Groovy. Groovy is a newer Java which compiles to Java byte code and you can mix-and-match Groovy and Java code in a script. The learning curve is not great if you know Java.
For me the question was: Why spend time doing MATLAB OOP if I can do it in a portable language and use the same the code in R, Scilab or a standalone application. My use-case, was a library of 2D graphics functions written in Java. These can be called from MATLAB but also from R, SciLab etc. Groovy's extra features, including dynamic typing, has made it much easier to provide a consistent API for each of those environments.
How is the OOP performance of groovy compared to MATLAB's OOP performance?
@Walter
In short, I do not know because I have not tested that. Groovy's dynamic features mean that it is generally slower that Java - using reflection etc much as MATLAB does for Java use. I have used it only to write a static library to interface to a Java API. That static library is called from MATLAB/R/Scilab etc.
As it produces Java byte code, MATLAB is always going to beat it in performance when the object methods are computationally intensive. Horses-for-courses: for matrix algebra use MATLAB.
But take the following snippet from the innards of a switch block which looks very MATLAB-like:
for (obj in props) {
switch (obj.key) {
...
case 'Alpha':
case 'EdgeColor':
case 'LineColor':
case 'XData':
case 'YData':
case 'ZData':
plot.("set" + obj.key)(props.(obj.key))
break
...
Here plot is a Java object which has methods such as setAlpha. props is a LinkedHashMap and its key set is being looped over. if map.key contains "Alpha" we call setAlpha using the value for that key form the map.
I think MATLAB programmers will find that this code looks pretty familiar. Java programmers would have needed to do a lot of tedious getClass()/getMethod() calls and probably included half-a-dozen try/catch blocks. Groovy does all that behind the scenes.
There are drawbacks - fewer compile time errors with Groovy than Java (so more chance of a run-time error) and they seem to have thrown out the Java good-practice handbook: properties and methods public by default for example. Also the IDEs are not all up-to-speed on it (IntelliJ seems best at present).
@Malcolm - could I shamelessly solicit you for a guest article about this for the UndocumentedMatlab.com website?
@Yair - no problem.
With a name like groovy, I feel compelled to learn it.

Sign in to comment.

Answers (1)

Yair Altman
Yair Altman on 4 Jul 2012
For anyone's information, I just posted the following article by Malcolm about using Groovy in Matlab: http://UndocumentedMatlab.com/blog/using-groovy-in-matlab/

Categories

Products

Asked:

on 1 Jul 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!