Skip to content

Commit 2106890

Browse files
committed
Convert to MatlabNumericArray if possible
When setting values in the MATLABBindings, only MatlabNumericArrays can be passed to MATLAB because it is its own JVM (so the pass is by value, but there is currently no alternative). Thus if we are not running inside MATLAB we should always try to convert objects using the ConvertService.
1 parent 6c54771 commit 2106890

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

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

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import matlabcontrol.extensions.MatlabNumericArray;
4646
import matlabcontrol.extensions.MatlabTypeConverter;
4747

48+
import org.scijava.convert.ConvertService;
4849
import org.scijava.log.LogService;
4950
import org.scijava.options.OptionsService;
5051
import org.scijava.plugin.Parameter;
@@ -59,6 +60,9 @@ public class MATLABBindings implements Bindings {
5960
@Parameter
6061
private OptionsService optionsService;
6162

63+
@Parameter
64+
private ConvertService convertService;
65+
6266
@Parameter
6367
private LogService logService;
6468

@@ -115,18 +119,29 @@ public Object put(final String name, final Object value) {
115119
final MatlabProxy proxy = MATLABControlUtils.proxy(opts());
116120

117121
// Try special MATLAB data types
118-
if (value != null &&
119-
MatlabNumericArray.class.isAssignableFrom(value.getClass()))
120-
{
122+
if (value != null) {
123+
MatlabNumericArray arrayVal = null;
124+
125+
// Cast if able
126+
if (MatlabNumericArray.class.isAssignableFrom(value.getClass())) {
127+
arrayVal = (MatlabNumericArray) value;
128+
}
129+
// Convert if able
130+
else if (convertService.supports(value, MatlabNumericArray.class)) {
131+
arrayVal = convertService.convert(value, MatlabNumericArray.class);
132+
}
133+
121134
// Convert the dataset to a MATLAB array and set it as a local variable
122135
// within MATLAB.
123-
final MatlabTypeConverter converter = new MatlabTypeConverter(proxy);
124-
try {
125-
converter.setNumericArray(sanitize(name), (MatlabNumericArray) value);
126-
return value;
127-
}
128-
catch (final MatlabInvocationException e) {
129-
logService.warn(e);
136+
if (arrayVal != null) {
137+
final MatlabTypeConverter converter = new MatlabTypeConverter(proxy);
138+
try {
139+
converter.setNumericArray(sanitize(name), arrayVal);
140+
return value;
141+
}
142+
catch (final MatlabInvocationException e) {
143+
logService.warn(e);
144+
}
130145
}
131146
}
132147

0 commit comments

Comments
 (0)