4141import javax .script .Bindings ;
4242
4343import matlabcontrol .MatlabInvocationException ;
44+ import matlabcontrol .MatlabProxy ;
4445import matlabcontrol .extensions .MatlabNumericArray ;
4546import matlabcontrol .extensions .MatlabTypeConverter ;
4647
48+ import org .scijava .options .OptionsService ;
49+ import org .scijava .plugin .Parameter ;
50+
4751/**
4852 * A {@link Bindings} wrapper around MATLAB's local variables.
4953 *
5054 * @author Mark Hiner
5155 */
5256public class MATLABBindings implements Bindings {
5357
58+ @ Parameter
59+ private OptionsService optionsService ;
60+
5461 @ Override
5562 public int size () {
5663 return getVars ().length ;
@@ -69,7 +76,7 @@ public boolean containsValue(final Object value) {
6976 @ Override
7077 public void clear () {
7178 try {
72- MATLABControlUtils .proxy ().eval ("clear" );
79+ MATLABControlUtils .proxy (opts () ).eval ("clear" );
7380 }
7481 catch (final MatlabInvocationException e ) {}
7582 }
@@ -101,26 +108,31 @@ public Set<java.util.Map.Entry<String, Object>> entrySet() {
101108
102109 @ Override
103110 public Object put (final String name , final Object value ) {
111+ final MatlabProxy proxy = MATLABControlUtils .proxy (opts ());
112+
113+ // Try special MATLAB data types
114+ if (MatlabNumericArray .class .isAssignableFrom (value .getClass ())) {
115+ // Convert the dataset to a MATLAB array and set it as a local variable
116+ // within MATLAB.
117+ final MatlabTypeConverter converter =
118+ new MatlabTypeConverter (proxy );
119+ try {
120+ converter .setNumericArray (sanitize (name ), (MatlabNumericArray ) value );
121+ return value ;
122+ }
123+ catch (final MatlabInvocationException e ) {
124+ System .err .println (e .getStackTrace ());
125+ }
126+ }
127+
104128 try {
105- MATLABControlUtils . proxy () .setVariable (sanitize (name ), value );
129+ proxy .setVariable (sanitize (name ), value );
106130 return value ;
107131 }
108132 catch (final MatlabInvocationException e ) {
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 (final MatlabInvocationException e1 ) {
120- throw new IllegalArgumentException (e );
121- }
122- }
133+ System .err .println (e .getStackTrace ());
123134 }
135+
124136 return null ;
125137 }
126138
@@ -160,24 +172,27 @@ private Object retrieveValue(final Object key, final boolean remove) {
160172 if (key instanceof String ) k = (String ) key ;
161173 else return null ;
162174 Object v = null ;
175+ final MatlabProxy proxy = MATLABControlUtils .proxy (opts ());
163176
177+ // Attempt to retrieve special MATLAB types
164178 try {
165- v = MATLABControlUtils .proxy ().getVariable (k );
166- if (remove ) MATLABControlUtils .proxy ().eval ("clear " + k );
179+ // try recovering key as a MatlabNumericArray
180+ final MatlabTypeConverter converter =
181+ new MatlabTypeConverter (proxy );
182+ v = converter .getNumericArray (k );
167183 }
168184 catch (final MatlabInvocationException e ) {
185+ System .err .println (e .getStackTrace ());
186+ }
169187
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- }
188+ try {
189+ v = proxy .getVariable (k );
190+ if (remove ) proxy .eval ("clear " + k );
191+ }
192+ catch (final MatlabInvocationException e ) {
193+ System .err .println (e .getStackTrace ());
180194 }
195+
181196 return v ;
182197 }
183198
@@ -187,7 +202,7 @@ private Object retrieveValue(final Object key, final boolean remove) {
187202 private String [] getVars () {
188203 try {
189204 final String [] vars =
190- (String []) MATLABControlUtils .proxy ().returningEval ("who" , 1 )[0 ];
205+ (String []) MATLABControlUtils .proxy (opts () ).returningEval ("who" , 1 )[0 ];
191206 return vars ;
192207 }
193208 catch (final MatlabInvocationException e ) {}
@@ -203,4 +218,13 @@ private String[] getVars() {
203218 private String sanitize (final String name ) {
204219 return name .replaceAll ("[^\\ w]" , "_" );
205220 }
221+
222+ /**
223+ * Convenience method for access to the {@link MATLABOptions}
224+ *
225+ * @return Active {@code MATLABOptions}
226+ */
227+ private MATLABOptions opts () {
228+ return optionsService .getOptions (MATLABOptions .class );
229+ }
206230}
0 commit comments