<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Groovy &#8211; Undocumented Matlab</title>
	<atom:link href="https://undocumentedmatlab.com/articles/tag/groovy/feed" rel="self" type="application/rss+xml" />
	<link>https://undocumentedmatlab.com</link>
	<description>Professional Matlab consulting, development and training</description>
	<lastBuildDate>Wed, 04 Jul 2012 19:14:02 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.3</generator>
	<item>
		<title>Using Groovy in Matlab</title>
		<link>https://undocumentedmatlab.com/articles/using-groovy-in-matlab?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-groovy-in-matlab</link>
					<comments>https://undocumentedmatlab.com/articles/using-groovy-in-matlab#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 04 Jul 2012 19:14:02 +0000</pubDate>
				<category><![CDATA[Guest bloggers]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Low risk of breaking in future versions]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Malcolm Lidierth]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=2988</guid>

					<description><![CDATA[<p>Groovy code can seamlessly be run from within Matlab. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/using-groovy-in-matlab">Using Groovy in Matlab</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/matlab-java-interface-using-static-control" rel="bookmark" title="Matlab-Java interface using a static control">Matlab-Java interface using a static control </a> <small>The switchyard function design pattern can be very useful when setting Matlab callbacks to Java GUI controls. This article explains why and how....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/jmi-java-to-matlab-interface" rel="bookmark" title="JMI &#8211; Java-to-Matlab Interface">JMI &#8211; Java-to-Matlab Interface </a> <small>JMI enables calling Matlab functions from within Java. This article explains JMI's core functionality....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/converting-java-vectors-to-matlab-arrays" rel="bookmark" title="Converting Java vectors to Matlab arrays">Converting Java vectors to Matlab arrays </a> <small>Converting Java vectors to Matlab arrays is pretty simple - this article explains how....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/using-sqlite-in-matlab" rel="bookmark" title="Using SQLite in Matlab">Using SQLite in Matlab </a> <small>SQLite databases can be accessed in a variety of different ways in Matlab. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p><i>Once again I would like to welcome guest blogger <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/authors/23816">Malcolm Lidierth</a> of King&#8217;s College London, who has already <a target="_blank" href="/articles/tag/malcolm-lidierth/">written here</a> in the past. Today Malcolm will explain the basics of using Groovy code within Matlab.</i><br />
Readers of this blog are likely to be familiar with calling Java from within Matlab code and/or writing custom Java code for Matlab use. But, Java is only one of many programming languages that can use the Java Virtual Machine. Here, I&#8217;d like to draw Matlab-users&#8217; attention to another: Groovy.<br />
<span class="alignright"><img decoding="async" title="Groovy logo" src="https://undocumentedmatlab.com/images/groovy-logo.png" alt="Groovy logo" width="227" height="112"/></span> <a target="_blank" rel="nofollow" href="http://en.wikipedia.org/wiki/Groovy_%28programming_language%29">Groovy</a> is a superset of Java that compiles to Java byte-code and seamlessly integrates with existing Java code. So seamlessly, in fact, that you can mix-and-match Java and Groovy syntax in a single file or method. This means that you can call Groovy classes from Matlab just as though they were Java classes: Matlab will see no difference.<br />
Most IDEs support Groovy and include the necessary files – IntelliJ seems particularly good for Groovy development. Groovy is also available separately at <a target="_blank" rel="nofollow" href="http://groovy.codehaus.org/Download">http://groovy.codehaus.org/Download</a>. Use Groovy 1.8.6 or earlier, not the latest 2.0 version (* see <a href="/articles/using-groovy-in-matlab/#Groovy2.0">below</a>).<br />
All that is needed to run Groovy in Matlab is to include its jar files on your Matlab javaclasspath, by using Matlab&#8217;s <i><b>javaaddpath</b></i> function to add to the dynamic classpath, or by adding the jar locations to the classpath.txt file (the static classpath, which is generally better).<br />
To illustrate a few of its features, below is a rough-and-ready static method in Groovy to return a factorial:</p>
<pre lang='groovy'>
package Examples
class StaticLib {
   static factorial(n){
      def fact=1.0
      for (def k=1; k< =n; k++)
         fact*=k
      return fact
   }
}
</pre>
<p>Java programmers will note there are no <code>public</code> keywords: in Groovy public is the default. Next the method declaration has no return type. Neither does the input <code>n</code>. In Groovy, typing is optional. This is exploited on the next line, where <code>fact</code> is declared using the <code>def</code> keyword. Groovy will determine its type at runtime much as Matlab does for variables. The same is true for the loop control variable <code>k</code>.<br />
To run the code at the Matlab command prompt, use commands as you would for Java e.g:</p>
<pre lang='matlab'>
>> import Examples.StaticLib
>> StaticLib.factorial(40)
>> StaticLib.factorial(int64(40))
>> StaticLib.factorial(java.math.BigInteger(40))
</pre>
<p>all produce:</p>
<pre lang='matlab'>
ans =
815915283247897734345611269596115894272000000000.0
</pre>
<p>This is an exact, and correct, result because Groovy instantiated <code>fact</code> as a <code>java.math.BigDecimal</code> when it was initialized with a floating-point value. Note also, that Groovy&#8217;s dynamic typing meant only one method was needed– not one for every possible class of input. With Groovy, you need to write much less code. (Caution: running this code with very large n will work, but slowly, and potentially take up all available memory to store the result).<br />
The small typing change below makes a substantial difference to the code&#8217;s operation:</p>
<pre lang='groovy'>
package Examples
   class NewLib {
      def factorial = {n ->
      def fact = 1.0
      for (def k = 1; k < = n; k++)
         fact *= k
      return fact
   }
}
</pre>
<p>Here, factorial is a property, not a method, of the class and its contents are the code – in Groovy this is a closure (in Matlab it would be called an anonymous function).<br />
Create an instance of the <code>NewLib</code> class in Matlab, call the <i>get</i> method on the <code>factorial</code> property and run the code in Matlab as follows:</p>
<pre lang='matlab'>
>> myObj=Examples.NewLib();
>> func=myObj.getFactorial();
>> func.call(40)
ans =
815915283247897734345611269596115894272000000000.0
</pre>
<p>Using Groovy saved a lot of work: there was no need to write a no argument constructor or a <i>getFactorial()</i> method. Groovy did that automatically.<br />
Here is another code snippet where Groovy&#8217;s dynamic typing makes it look much more like Matlab than Java:</p>
<pre lang='groovy'>
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
      …
   }
}
</pre>
<p>props is a <code>LinkedHashMap</code>. We start by running through each of the entries in this map in a loop using:</p>
<pre lang='groovy'>for (obj in props)</pre>
<p>Each entry is assigned to <code>obj</code> in turn. Again, we let Groovy determine the class of <code>obj</code> dynamically &#8211; it will be of class <code>java.util.LinkedHashMap$Entry</code>, but we do not need to worry about that detail.<br />
For each entry we retrieve the key and use that as the variable for the <code>switch</code> block. In this case, the keys are all strings and, in Groovy, strings can be used in <code>case</code> statements (Java 7 would be needed for that if the code were written in Java; note that Java 7 is still not integrated in Matlab as of this date).<br />
Within the switch block, the code invokes a setter on an object called <code>plot</code>. Rather than write separate lines for each possible property in the key list, one line is enough:</p>
<pre lang='groovy'>plot.("set" + obj.key)(props.(obj.key))</pre>
<p>The key string is pre-pended with &#8220;set&#8221; and the corresponding method is invoked passing the value from the <code>LinkedHashMap</code> entry as input – so a particular iteration of the loop, this might equate for example to <code>plot.setAlpha(0.5)</code><br />
For a Matlab programmer who is put off from delving into Java because of the tedious boiler-plate code it requires, Groovy may be an attractive alternative. Groovy also has many powerful features for handling regular expressions, a set of builder classes (for example a thread-safe <code>SwingBuilder</code> class), writing domain specific languages and for meta-object programming including run-time injection of new methods. There is also a growing set of Groovy <a target="_blank" rel="nofollow" href="http://groovy.codehaus.org/Modules">plugin modules</a>.<br />
Not all Java programmers like Groovy. The dynamic features inevitably create run-time overheads and mean that type-mismatches that would be detected at compile-time in Java may produce run-time exceptions with Groovy, but Groovy 2.0 addresses some of these issues by introducing new <a target="_blank" rel="nofollow" href="http://www.infoq.com/articles/new-groovy-20">compiler annotations</a>. My own use of it has been limited, partly because of these issues: I have used it to create a static library to link Matlab, R, SciLab etc to a graphics package written in Java. The dynamic features of Groovy have been useful there to reduce the work involved.<br />
<a id="Groovy2.0"></a><br />
<i>* Note: Groovy 2.0 was released 28.06.2012. Initial experiments suggest it does not work within Matlab, perhaps because of version clashes with jars on the Matlab static class path.</i><br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<div style="background-color: rgb(255, 255, 0);">
Note: in 21-23 August 2012, I (Yair) will present advanced Matlab training courses in <b>Geneva</b>, Switzerland. The planned topics are:</p>
<ul>
<li>Aug 21 &#8211; <a target="_blank" href="/courses/Matlab_Performance_Tuning_Course.pdf">Matlab performance tuning</a></li>
<li>Aug 22 &#8211; <a target="_blank" href="/courses/Advanced_Matlab_GUI_Course_1d.pdf">Advanced Matlab GUI</a></li>
<li>Aug 23 &#8211; <a target="_blank" href="/courses/Using_Java_in_Matlab_Course.pdf">Using Java in Matlab</a> (inc. extra advanced GUI)</li>
</ul>
<p>Anyone interested please <a href="mailto:%20altmany%20@gmail.com?subject=Matlab%20courses&amp;body=Hi%20Yair,%20&amp;cc=;&amp;bcc=" rel="nofollow" target="_blank" onclick="var n='altmany'; var d='gmail.com'; window.open('mailto:'+n+'@'+d+'?subject=Matlab courses&amp;body=Hi Yair, '); return false;">email me</a> (altmany at gmail dot com) for more details.
</div>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/using-groovy-in-matlab">Using Groovy in Matlab</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/matlab-java-interface-using-static-control" rel="bookmark" title="Matlab-Java interface using a static control">Matlab-Java interface using a static control </a> <small>The switchyard function design pattern can be very useful when setting Matlab callbacks to Java GUI controls. This article explains why and how....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/jmi-java-to-matlab-interface" rel="bookmark" title="JMI &#8211; Java-to-Matlab Interface">JMI &#8211; Java-to-Matlab Interface </a> <small>JMI enables calling Matlab functions from within Java. This article explains JMI's core functionality....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/converting-java-vectors-to-matlab-arrays" rel="bookmark" title="Converting Java vectors to Matlab arrays">Converting Java vectors to Matlab arrays </a> <small>Converting Java vectors to Matlab arrays is pretty simple - this article explains how....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/using-sqlite-in-matlab" rel="bookmark" title="Using SQLite in Matlab">Using SQLite in Matlab </a> <small>SQLite databases can be accessed in a variety of different ways in Matlab. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/using-groovy-in-matlab/feed</wfw:commentRss>
			<slash:comments>8</slash:comments>
		
		
			</item>
	</channel>
</rss>
