From 4f223537eee46c065db9ea56cb122aa222103091 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Sat, 14 Feb 2015 16:50:36 -0600 Subject: [PATCH 1/3] Add more tests for nargout and tests for tuple args --- pymatbridge/tests/test_run_code.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/pymatbridge/tests/test_run_code.py b/pymatbridge/tests/test_run_code.py index 7a86dcc..2b7714f 100644 --- a/pymatbridge/tests/test_run_code.py +++ b/pymatbridge/tests/test_run_code.py @@ -71,9 +71,26 @@ def test_nargout(self): U, S, V = res['result'] npt.assert_almost_equal(U, np.array([[-0.57604844, -0.81741556], [-0.81741556, 0.57604844]])) - + npt.assert_almost_equal(S, np.array([[ 3.86432845, 0.], [ 0., 0.25877718]])) npt.assert_almost_equal(V, np.array([[-0.36059668, -0.93272184], [-0.93272184, 0.36059668]])) + + res = self.mlab.run_func('svd', np.array([[1,2],[1,3]]), nargout=1) + s = res['result'] + npt.assert_almost_equal(s, [[ 3.86432845], [ 0.25877718]]) + + res = self.mlab.run_func('close', 'all', nargout=0) + assert res['result'] == [] + + def test_tuple_args(self): + res = self.mlab.run_func('ones', (1, 2)) + npt.assert_almost_equal(res['result'], [[1, 1]]) + + res = self.mlab.run_func('chol', + (np.array([[2, 2], [1, 1]]), 'lower')) + npt.assert_almost_equal(res['result'], + [[1.41421356, 0.], + [0.70710678, 0.70710678]]) From 06c01aefcb1cd0449fc350aa9edc5a52bb248ccb Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Sat, 14 Feb 2015 16:50:55 -0600 Subject: [PATCH 2/3] Add handling of cell arguments and update documentation --- pymatbridge/matlab/util/run_dot_m.m | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/pymatbridge/matlab/util/run_dot_m.m b/pymatbridge/matlab/util/run_dot_m.m index 013f6a5..e60aeac 100644 --- a/pymatbridge/matlab/util/run_dot_m.m +++ b/pymatbridge/matlab/util/run_dot_m.m @@ -1,12 +1,13 @@ % Max Jaderberg 2011 -function varargout = run_dot_m( file_to_run, arguments, nout ) -%RUN_DOT_M Runs the given .m file with the argument struct given -% For exmaple run_dot_m('/path/to/function.m', args); -% args is a struct containing the arguments. function.m must take only -% one parameter, the argument structure +function varargout = run_dot_m( func_to_run, arguments, nout ) +%RUN_DOT_M Runs the given function or .m file with the arguments given +% and the nout selected +% For exmaple run_dot_m('/path/to/function.m', args, 1); +% arguments can be a scalar, as cell, or struct containing the arguments. +% If it is a struct, func_to_run must take only one parameter, the argument structure - [dname, func_name, ext] = fileparts(file_to_run); + [dname, func_name, ext] = fileparts(func_to_run); if size(ext) if ~strcmp(ext, '.m') @@ -20,6 +21,10 @@ addpath(dname); end - [varargout{1:nout}] = feval(func_name, arguments); + if iscell(arguments) + [varargout{1:nout}] = feval(func_name, arguments{:}); + else + [varargout{1:nout}] = feval(func_name, arguments); + end end From 4c22e3e54380939c6a0b58890f9a479493ddeb9c Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Sat, 14 Feb 2015 16:51:23 -0600 Subject: [PATCH 3/3] Update documentation --- pymatbridge/pymatbridge.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/pymatbridge/pymatbridge.py b/pymatbridge/pymatbridge.py index b6310bc..543bc26 100644 --- a/pymatbridge/pymatbridge.py +++ b/pymatbridge/pymatbridge.py @@ -263,15 +263,35 @@ def is_function_processor_working(self): def _json_response(self, **kwargs): return json.loads(self._response(**kwargs), object_hook=decode_pymat) - # Run a function in Matlab and return the result def run_func(self, func_path, func_args=None, nargout=1): + """Run a function in Matlab and return the result. + + Parameters + ---------- + func_path: str + Name of function to run or a path to an m-file. + func_args: object + Function args to send to the function. + nargout: int + Desired number of return arguments. + + Returns + ------- + Result dictionary with keys: 'message', 'result', and 'success' + """ return self._json_response(cmd='run_function', func_path=func_path, func_args=func_args, nargout=nargout) - # Run some code in Matlab command line provide by a string def run_code(self, code): + """Run some code in Matlab command line provide by a string + + Parameters + ---------- + code : str + Code to send for evaluation. + """ return self._json_response(cmd='run_code', code=code) def get_variable(self, varname, default=None):