diff --git a/pymatbridge/matlab/util/pymat_feval.m b/pymatbridge/matlab/util/pymat_feval.m index 9a74e13..0a86282 100644 --- a/pymatbridge/matlab/util/pymat_feval.m +++ b/pymatbridge/matlab/util/pymat_feval.m @@ -31,7 +31,12 @@ arguments = ''; end - response.result = run_dot_m(func_path, arguments); + [resp{1:req.nargout}] = run_dot_m(func_path, arguments, req.nargout); + if req.nargout == 1 + response.result = resp{1}; + else + response.result = resp; + end response.success = 'true'; response.message = 'Successfully completed request'; diff --git a/pymatbridge/matlab/util/run_dot_m.m b/pymatbridge/matlab/util/run_dot_m.m index 7f78970..013f6a5 100644 --- a/pymatbridge/matlab/util/run_dot_m.m +++ b/pymatbridge/matlab/util/run_dot_m.m @@ -1,28 +1,25 @@ % Max Jaderberg 2011 -function result = run_dot_m( file_to_run, arguments ) +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 - [dir, func_name, ext] = fileparts(file_to_run); + [dname, func_name, ext] = fileparts(file_to_run); - if ~size(ext) - result = 'Error: Need to give path to .m file'; - return + if size(ext) + if ~strcmp(ext, '.m') + varargout = 'Error: Need to give path to .m file'; + return + end end - if ~strcmp(ext, '.m') - result = 'Error: Need to give path to .m file'; - return + % Add function path to current path + if size(dname) + addpath(dname); end -% Add function path to current path - addpath(dir); - if isstruct(arguments) - result = feval(func_name, arguments); - else - result = feval(func_name); - end + [varargout{1:nout}] = feval(func_name, arguments); + end diff --git a/pymatbridge/pymatbridge.py b/pymatbridge/pymatbridge.py index 5e1be42..b6310bc 100644 --- a/pymatbridge/pymatbridge.py +++ b/pymatbridge/pymatbridge.py @@ -264,10 +264,11 @@ 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): + def run_func(self, func_path, func_args=None, nargout=1): return self._json_response(cmd='run_function', func_path=func_path, - func_args=func_args) + func_args=func_args, + nargout=nargout) # Run some code in Matlab command line provide by a string def run_code(self, code): diff --git a/pymatbridge/tests/test_run_code.py b/pymatbridge/tests/test_run_code.py index 6f929bb..7a86dcc 100644 --- a/pymatbridge/tests/test_run_code.py +++ b/pymatbridge/tests/test_run_code.py @@ -1,5 +1,6 @@ import pymatbridge as pymat from pymatbridge.compat import text_type +import numpy as np import numpy.testing as npt import test_utils as tu @@ -63,3 +64,16 @@ def test_undefined_code(self): npt.assert_equal(message, "'this_is_nonsense' undefined near line 1 column 1") else: npt.assert_equal(message, "Undefined function or variable 'this_is_nonsense'.") + + + def test_nargout(self): + res = self.mlab.run_func('svd', np.array([[1,2],[1,3]]), nargout=3) + 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]]))