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

Image Analyst
Image Analyst 2012년 7월 1일
편집: Image Analyst 2012년 7월 1일
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.
Malcolm Lidierth
Malcolm Lidierth 2012년 7월 1일
@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.
Walter Roberson
Walter Roberson 2012년 7월 1일
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).
Yair Altman
Yair Altman 2012년 7월 3일
@Malcolm - could I shamelessly solicit you for a guest article about this for the UndocumentedMatlab.com website?
Malcolm Lidierth
Malcolm Lidierth 2012년 7월 3일
@Yair - no problem.
Kye Taylor
Kye Taylor 2012년 7월 3일
With a name like groovy, I feel compelled to learn it.

댓글을 달려면 로그인하십시오.

답변 (1개)

Yair Altman
Yair Altman 2012년 7월 4일

0 개 추천

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/

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

태그

질문:

2012년 7월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by