Loading Groovy Scripts In Matlab
Groovy is a neat java like scripting language which is much neater for writing small scripts.The below code shows how to load groovy scripts into matlab. It involves registering the groovy classloader with the matlab classloader. ( Took me ages to figure this one out ).
function klass = groovy_load_class(script); import groovy.lang.*; import com.mathworks.jmi.*; % Locate the script on the matlab path script = which(script); % Get the system class loader p_loader = java.lang.ClassLoader.getSystemClassLoader; % Create the Groovy class loader loader = GroovyClassLoader(p_loader); % Register the class loader with the matlab % environments com.mathworks.jmi.OpaqueJavaInterface.registerClassLoader(loader); % Parse the groovy class klass = loader.parseClass(java.io.File(script));
It will find the script on the Matlab path and load it returning the java class handle of the new class. You can then create new instances of it as if it were a normal java class.
klass = groovy('MyGroovyClass.groovy'); h = com.xtargets.MyGroovyClass;