Skip to content

Commit 3d0994d

Browse files
committed
Convert retrieved values to double
We rely on MatlabControl to extract data from MATLAB and pass it to our current JVM (if different). Unfortunately, the MatlabNumericArray class can only wrap MATLAB double arrays. When unwrapped arrays are returned directly their dimensionality is lost (a 1D array is returned). So we must convert to double arrays using the MATLAB "double" function to ensure data is wrappable by MatlabControl.
1 parent 2106890 commit 3d0994d

1 file changed

Lines changed: 23 additions & 6 deletions

File tree

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

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,20 +198,37 @@ private Object retrieveValue(final Object key, final boolean remove) {
198198

199199
// Attempt to retrieve special MATLAB types
200200
try {
201+
// Only double arrays can be converted to MatlabNumericArrays.
202+
// This is a highly aggressive conversion but currently no known
203+
// alternative.
204+
final String command = k + " = double(" + k + ");";
205+
proxy.eval(command);
206+
201207
// try recovering key as a MatlabNumericArray
202208
final MatlabTypeConverter converter = new MatlabTypeConverter(proxy);
203209
v = converter.getNumericArray(k);
204210
}
205211
catch (final MatlabInvocationException e) {
206-
logService.warn(e);
212+
logService.warn("Could not convert: " + k +
213+
" to a MatlabNumericArray.\n\tDimensionality information may be lost.");
207214
}
208215

209-
try {
210-
v = proxy.getVariable(k);
211-
if (remove) proxy.eval("clear " + k);
216+
if (v == null) {
217+
try {
218+
v = proxy.getVariable(k);
219+
}
220+
catch (final MatlabInvocationException e) {
221+
logService.warn(e);
222+
}
212223
}
213-
catch (final MatlabInvocationException e) {
214-
logService.warn(e);
224+
225+
if (remove) {
226+
try {
227+
proxy.eval("clear " + k);
228+
}
229+
catch (MatlabInvocationException e) {
230+
logService.warn(e);
231+
}
215232
}
216233

217234
return v;

0 commit comments

Comments
 (0)