Skip to content

Commit bff978b

Browse files
committed
Fix up MATLAB ScriptEngine
* Removed unnecessary while loop * Instead of using ans, now store script in temporary variable.
1 parent 3a0362c commit bff978b

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed

src/main/java/org/scijava/plugins/scripting/matlab/MATLABScriptEngine.java

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.io.BufferedReader;
3535
import java.io.Reader;
3636
import java.io.StringReader;
37+
import java.util.Random;
3738

3839
import javax.script.ScriptException;
3940

@@ -98,45 +99,44 @@ public Object eval(final Reader reader) throws ScriptException {
9899
final MATLABOptions options =
99100
optionsService.getOptions(MATLABOptions.class);
100101
final MatlabProxy proxy = MATLABControlUtils.proxy(options);
101-
final Thread thread = Thread.currentThread();
102102
Object finalResult = null;
103103
try {
104-
while (!thread.isInterrupted()) {
105-
String command = "sprintf('";
106-
while (bufReader.ready()) {
107-
final String line = bufReader.readLine();
108-
if (line == null) break;
109-
110-
// NB: we have to manually exclude comment lines in MATLAB. Otherwise,
111-
// the newline characters themselves on the comment lines will be
112-
// commented out and ignored - resulting in the first true line of
113-
// code being skipped unintentionally.
114-
if (line.matches("^[^\\w]*" + COMMENT + ".*")) continue;
115-
command += line + "\\n";
116-
}
117-
command += "')";
118-
119-
// Evaluate the command
120-
// NB: the first eval turns a multi-line command into something properly
121-
// formatted for MATLAB, stored in the MATLAB variable "ans".
122-
// We then have to evaluate "ans". However, the eval methods of
123-
// MatlabControl force "eval(' + args + ')" and "eval('ans')" displays
124-
// the string literal "ans", whereas "eval(ans)" actually evaluates
125-
// whatever is stored in ans. We wan the latter behavior, thus the
126-
// nested eval.
127-
proxy.eval(command);
128-
129-
try {
130-
// Attempt to get a return value
131-
finalResult = proxy.returningEval("eval(ans)", 1);
132-
}
133-
catch (final MatlabInvocationException e) {
134-
// no return value. Just eval and be done.
135-
// NB: modified MATLAB variables can be accessed via the bindings.
136-
proxy.eval("eval(ans)");
137-
}
138-
break;
104+
final String scriptVar = "scijava_script" + new Random().nextInt(999999);
105+
String command = scriptVar + " = sprintf('";
106+
while (bufReader.ready()) {
107+
final String line = bufReader.readLine();
108+
if (line == null) break;
109+
110+
// NB: we have to manually exclude comment lines in MATLAB. Otherwise,
111+
// the newline characters themselves on the comment lines will be
112+
// commented out and ignored - resulting in the first true line of
113+
// code being skipped unintentionally.
114+
if (line.matches("^[^\\w]*" + COMMENT + ".*")) continue;
115+
command += line + "\\n";
139116
}
117+
command += "')";
118+
119+
// NB: this first eval turns a multi-line command into something properly
120+
// formatted for MATLAB, stored in a temporary MATLAB variable
121+
// We then have to evaluate this variable. However, the eval methods of
122+
// MatlabControl force "eval(' + args + ')" and "eval('var')" displays
123+
// the string literal "var", whereas "eval(var)" actually evaluates
124+
// whatever is stored in var. We want the latter behavior, thus the
125+
// need for a nested eval.
126+
proxy.eval(command);
127+
128+
// Perform nested eval.. with or without a return value
129+
try {
130+
// Attempt to get a return value
131+
finalResult = proxy.returningEval("eval(" + scriptVar + ")", 1);
132+
}
133+
catch (final MatlabInvocationException e) {
134+
// no return value. Just eval and be done.
135+
// NB: modified MATLAB variables can be accessed via the bindings.
136+
proxy.eval("eval(" + scriptVar + ")");
137+
}
138+
139+
proxy.eval("clearvars " + scriptVar);
140140
}
141141
catch (final Exception e) {}
142142

0 commit comments

Comments
 (0)