Skip to content

Commit 83db7c5

Browse files
committed
Move single-element array handling to bindings
Since we are already converting to MatlabNumericArrays in the bindings it makes more sense to handle single-element arrays there, instead of recapitulating logic to detect them in decode.
1 parent 258bec6 commit 83db7c5

File tree

2 files changed

+12
-23
lines changed

2 files changed

+12
-23
lines changed

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -206,25 +206,24 @@ private Object retrieveValue(final Object key, final boolean remove) {
206206
// Array types will lose dimensionality if simply called via getVariable.
207207
// We need to convert the object to a double array in MATLAB so we can use
208208
// the MatlabNumericArray class.
209-
// NB: we can allow singleton array wrappers to be unwrapped by the
210-
// MATLABScriptLanguage.decode method.
211-
// We can NOT perform this double conversion in decode, however, because it
209+
// NB: we can NOT perform this double conversion in decode because it
212210
// requires the variable to still exist in MATLAB (which is not guaranteed
213211
// by the time control passes to decode).
214212
if (v != null && v.getClass().isArray())
215213
{
216214
try {
217-
// Skip single element arrays
218-
if ((int)((double[]) proxy.returningEval("numel(" + k + ")", 1)[0])[0]
219-
!= 1)
220-
{
221-
final String command = k + " = double(" + k + ");";
222-
proxy.eval(command);
223-
224-
// try recovering key as a MatlabNumericArray
225-
final MatlabTypeConverter converter = new MatlabTypeConverter(proxy);
226-
v = converter.getNumericArray(k);
215+
final String command = k + " = double(" + k + ");";
216+
proxy.eval(command);
217+
218+
// try recovering key as a MatlabNumericArray
219+
final MatlabTypeConverter converter = new MatlabTypeConverter(proxy);
220+
final MatlabNumericArray array = converter.getNumericArray(k);
221+
222+
// Unwrap single element arrays to primitive array
223+
if (array.getLength() == 1) {
224+
v = new double[] { array.getRealValue(0) };
227225
}
226+
else v = array;
228227
}
229228
catch (final MatlabInvocationException e) {
230229
logService

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,6 @@ public List<String> getExtensions() {
5454
return Arrays.asList("m");
5555
}
5656

57-
@Override
58-
public Object decode(final Object o) {
59-
if (o != null && o instanceof Object[]) {
60-
final Object[] oArray = (Object[]) o;
61-
if (oArray.length == 1) return oArray[0];
62-
}
63-
64-
return o;
65-
}
66-
6757
@Override
6858
public ScriptEngine getScriptEngine() {
6959
return new MATLABScriptEngine(getContext());

0 commit comments

Comments
 (0)