Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More nargout tests and handle tuple arguments #137

Merged
merged 3 commits into from
Feb 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions pymatbridge/matlab/util/run_dot_m.m
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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
24 changes: 22 additions & 2 deletions pymatbridge/pymatbridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
19 changes: 18 additions & 1 deletion pymatbridge/tests/test_run_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]])