Skip to content

Commit 36bbf0f

Browse files
committed
Trade MatlabNumericArrays with MATLAB
If we get a MatlabNumericArray, we will now use the MatlabControl library to convert with the underlying MATLAB array type (in both put and get directions).
1 parent aa3758d commit 36bbf0f

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
import javax.script.Bindings;
4242

4343
import matlabcontrol.MatlabInvocationException;
44+
import matlabcontrol.extensions.MatlabNumericArray;
45+
import matlabcontrol.extensions.MatlabTypeConverter;
4446

4547
/**
4648
* A {@link Bindings} wrapper around MATLAB's local variables.
@@ -104,7 +106,20 @@ public Object put(final String name, final Object value) {
104106
return value;
105107
}
106108
catch (final MatlabInvocationException e) {
107-
// TODO throw exception/print error -
109+
110+
// Try special MATLAB data types
111+
if (MatlabNumericArray.class.isAssignableFrom(value.getClass())) {
112+
// Convert the dataset to a MATLAB array and set it as a local variable
113+
// within MATLAB.
114+
final MatlabTypeConverter converter =
115+
new MatlabTypeConverter(MATLABControlUtils.proxy());
116+
try {
117+
converter.setNumericArray(name, (MatlabNumericArray) value);
118+
}
119+
catch (MatlabInvocationException e1) {
120+
throw new IllegalArgumentException(e);
121+
}
122+
}
108123
}
109124
return null;
110125
}
@@ -150,7 +165,19 @@ private Object retrieveValue(final Object key, final boolean remove) {
150165
v = MATLABControlUtils.proxy().getVariable(k);
151166
if (remove) MATLABControlUtils.proxy().eval("clear " + k);
152167
}
153-
catch (final MatlabInvocationException e) {}
168+
catch (final MatlabInvocationException e) {
169+
170+
// Attempt to retrieve special MATLAB types
171+
try {
172+
// try recovering key as a MatlabNumericArray
173+
final MatlabTypeConverter converter =
174+
new MatlabTypeConverter(MATLABControlUtils.proxy());
175+
v = converter.getNumericArray(k);
176+
}
177+
catch (final MatlabInvocationException e1) {
178+
System.err.println(e.getStackTrace());
179+
}
180+
}
154181
return v;
155182
}
156183

0 commit comments

Comments
 (0)