The GShell class
When Waterloo is installed, classes supplied as part of the Groovy runtime are also accessible directly from MATLAB.
The GShell supplements this with a set of static methods for compiling and
running scripts written purely in Java or Groovy. This provides full access
to these languages from MATLAB including access to inner classes and
features such as thread control.
For example, using GShell we can create a
Rectangle2D.Double instance easily:
>> import kcl.waterloo.shell.GShell >> GShell.eval('new java.awt.geom.Rectangle2D.Double(0d,0d,10d,10d)') ans = java.awt.geom.Rectangle2D$Double[x=0.0,y=0.0,w=10.0,h=10.0]You can create anonymous functions (called Closures in Groovy and use them multiple times in the script:
>> GShell.eval('Closure printSum = { a, b -> return a+b }; printSum(10,2)') ans = 12Creating Waterloo charts is easy:
>> p = GShell.eval('import kcl.waterloo.plot.*; WPlot.scatter(["XData",1..10,"YData",1..10])') p = kcl.waterloo.plot.WPlot@72e1c560 >> p.createFrame()You can also load, compile and run .groovy scripts from file. These can be created in MATLAB, or with one of the many freely available Java/Groocy IDEs such as NetBeans, Eclipse or IntelliJ.
For Groovy, I find IntelliJ has the best code completion support and it includes useful suggestions about writing well-styled Groovy code.
Among the advantages of using .groovy scripts for MATLAB programmers is:
- They can be incorporated into a Groovy/Java application fairly directly and compiled as part of the project jar file
- Scripts can be run in other environments such as SciLab.