/** * If the specified attribute is <i>URL</i>, then close the current file (if there is one) and * open the new one. * * @param attribute The attribute that has changed. * @exception IllegalActionException If the specified attribute is <i>URL</i> and the file cannot * be opened. */ @Override public void attributeChanged(Attribute attribute) throws IllegalActionException { if (attribute == sourceURL) { try { StringToken URLToken = (StringToken) sourceURL.getToken(); if (URLToken == null) { _source = null; setReader(null); } else { _source = URLToken.stringValue(); if (_source.equals("")) { setReader(null); } else { URL url = new URL(_source); java.io.BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); setReader(reader); } } } catch (IOException ex) { throw new IllegalActionException(this, ex, "attributeChanged(" + attribute + ") failed"); } } super.attributeChanged(attribute); }
/** Initialize the component and connect to the ORB. */ public void initialize() throws IllegalActionException { try { super.initialize(); _orb = new Orb(StringToken.convert(orbname.getToken()).stringValue(), "r"); _orb.select(StringToken.convert(srcname.getToken()).stringValue()); _orb.after(0); } catch (Exception e) { throw new IllegalActionException( this, "Couldn't connect to Orb." + " (" + e.getMessage() + ")"); } }
/** * Invoke the Matlab engine and run a few tests. * * @param args Not used. * @exception IllegalActionException If there is a problem running the tests. */ public static void main(String[] args) throws IllegalActionException { Engine eng = new Engine(); eng.setDebugging((byte) 0); long[] engineHandle = eng.open(); eng.evalString(engineHandle, "clear"); DoubleMatrixToken tx = new DoubleMatrixToken(new double[][] {{1, 2, 3}}); System.out.println("\nNote: All data output is via " + "Token.toString() on tokens"); System.out.println("that are put/get from the matlab engineHandle."); System.out.println("\nCreate 1x3 double matrix x:"); eng.put(engineHandle, "x", tx); System.out.println("x = " + tx.toString()); System.out.println("Eval: y = x.*x;"); eng.evalString(engineHandle, "y = x.*x;"); DoubleMatrixToken ty = (DoubleMatrixToken) eng.get(engineHandle, "y"); System.out.println("y = " + ty.toString()); System.out.println("\nCreate 2x3 double matrix x:"); tx = new DoubleMatrixToken(new double[][] {{1, 2, 3}, {4, 5, 6}}); eng.put(engineHandle, "x", tx); System.out.println("x = " + tx.toString()); System.out.println("Eval: y = x.*x;"); eng.evalString(engineHandle, "y = x.*x;"); ty = (DoubleMatrixToken) eng.get(engineHandle, "y"); System.out.println("y = " + ty.toString()); System.out.println("\nEval: z = exp(j*pi/2*x);"); eng.evalString(engineHandle, "z = exp(j*pi/2*x);"); ComplexMatrixToken tz = (ComplexMatrixToken) eng.get(engineHandle, "z"); System.out.println("z = " + tz.toString()); System.out.println("\nEval: w = z';"); eng.evalString(engineHandle, "w = z';"); ComplexMatrixToken tw = (ComplexMatrixToken) eng.get(engineHandle, "w"); System.out.println("w = " + tw.toString()); System.out.println("\nCreate 1xn string s:"); StringToken ts = new StringToken("a string"); System.out.println("s = " + ts.toString()); eng.put(engineHandle, "s", ts); System.out.println("\nEval: rc = [s;s];"); eng.evalString(engineHandle, "rc = [s;s];"); Token ta = eng.get(engineHandle, "rc"); System.out.println("rc = " + ta.toString()); System.out.println("\nCreate 2xn string s:"); ta = new ArrayToken(new Token[] {new StringToken("str one"), new StringToken("str two")}); System.out.println("s = " + ta.toString()); eng.put(engineHandle, "s", ta); System.out.println("\nEval: rr = [s,s];"); eng.evalString(engineHandle, "rr = [s,s];"); ta = eng.get(engineHandle, "rr"); System.out.println("rr = " + ta.toString()); System.out.println("\nCreate 1x1 struct r (RecordToken):"); RecordToken tr = new RecordToken( new String[] {"x", "r", "s"}, new Token[] { tx, new RecordToken(new String[] {"a"}, new Token[] {new IntToken()}), ts }); System.out.println("r = " + tr.toString()); eng.put(engineHandle, "r", tr); Token t = eng.get(engineHandle, "r"); System.out.println("\nRead back 1x1 struct r into RecordToken t:"); System.out.println("t = " + t.toString()); System.out.println("\nEval: ta = [r,r,r;r,r,r];"); eng.evalString(engineHandle, "ta = [r,r,r;r,r,r];"); t = eng.get(engineHandle, "ta"); System.out.println("\nRead 2x3 struct ta into ArrayToken " + "of ArrayToken of RecordTokens:"); System.out.println("ta = " + t.toString()); System.out.println("\nCreate 1x3 cell array from ta, " + "an ArrayToken of RecordTokens:"); RecordToken r1 = new RecordToken( new String[] {"a"}, new Token[] { new ComplexMatrixToken( new Complex[][] {{new Complex(1.0, 1.0), new Complex(2.0, 2.0)}}) }); RecordToken r2 = new RecordToken( new String[] {"a"}, new Token[] { new ComplexMatrixToken( new Complex[][] {{new Complex(3.0, 3.0), new Complex(4.0, 4.0)}}) }); RecordToken r3 = new RecordToken( new String[] {"a"}, new Token[] { new ComplexMatrixToken( new Complex[][] {{new Complex(5.0, 5.0), new Complex(6.0, 6.0)}}) }); ta = new ArrayToken(new Token[] {r1, r2, r3}); System.out.println("ta = " + ta.toString()); eng.put(engineHandle, "ta", ta); eng.evalString(engineHandle, "tb = ta;"); ta = eng.get(engineHandle, "tb"); System.out.println("\nRead 1x3 cell array back into tb:"); System.out.println("tb = " + ta.toString()); eng.close(engineHandle); }