Skip to content
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
10 changes: 1 addition & 9 deletions pymatbridge/matlab/matlabserver.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,10 @@ function matlabserver(socket_address)
clear mex;
break;

case {'run_function'}
resp = pymat_feval(req);
messenger('respond', resp);

case {'run_code'}
case {'eval'}
resp = pymat_eval(req);
messenger('respond', resp);

case {'get_var'}
resp = pymat_get_variable(req);
messenger('respond', resp);

otherwise
messenger('respond', 'i dont know what you want');
end
Expand Down
47 changes: 22 additions & 25 deletions pymatbridge/matlab/util/pymat_eval.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,32 @@
%
% Based on Max Jaderberg's web_feval

response.success = 'false';
field_names = fieldnames(req);

response.success = true;
response.content = '';

code_check = false;
if size(field_names)
if isfield(req, 'code')
code_check = true;
end
end

if ~code_check
response.message = 'No code provided as POST parameter';
json_response = json_dump(response);
return;
end

code = req.code;
response.result = '';

try
% tempname is less likely to get bonked by another process.
diary_file = [tempname() '_diary.txt'];
diary(diary_file);
evalin('base', code);

% Add function path to current path
if req.dname
addpath(req.dname);
end

if iscell(req.func_args)
[resp{1:req.nargout}] = feval(req.func_name, req.func_args{:});
else
[resp{1:req.nargout}] = feval(req.func_name, req.func_args);
end

if req.nargout == 1
response.result = resp{1};
else
response.result = resp;
end

diary('off');

datadir = fullfile(tempdir(),'MatlabData');
Expand All @@ -45,8 +46,6 @@
end

fig_files = make_figs(datadir);

response.success = 'true';
response.content.figures = fig_files;

% this will not work on Windows:
Expand All @@ -58,18 +57,16 @@
fclose(FID);
response.content.stdout = stdout;
else
response.success = 'false';
response.success = false;
response.content.stdout = sprintf('could not open %s for read',diary_file);
end
delete(diary_file)
catch ME
diary('off');
response.success = 'false';
response.success = false;
response.content.stdout = ME.message;
end

response.content.code = code;

json_response = json_dump(response);

end %function
54 changes: 0 additions & 54 deletions pymatbridge/matlab/util/pymat_feval.m

This file was deleted.

43 changes: 0 additions & 43 deletions pymatbridge/matlab/util/pymat_get_variable.m

This file was deleted.

7 changes: 0 additions & 7 deletions pymatbridge/matlab/util/pymat_set_variable.m

This file was deleted.

30 changes: 0 additions & 30 deletions pymatbridge/matlab/util/run_dot_m.m

This file was deleted.

4 changes: 2 additions & 2 deletions pymatbridge/matlab_magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def eval(self, line):
"""
run_dict = self.Matlab.run_code(line)

if run_dict['success'] == 'false':
if not run_dict['success']:
raise MatlabInterperterError(line, run_dict['content']['stdout'])

# This is the matlab stdout:
Expand All @@ -105,7 +105,7 @@ def set_matlab_var(self, name, value):
"""
run_dict = self.Matlab.set_variable(name, value)

if run_dict['success'] == 'false':
if not run_dict['success']:
raise MatlabInterperterError(line, run_dict['content']['stdout'])


Expand Down
25 changes: 15 additions & 10 deletions pymatbridge/pymatbridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
.MATLAB started and connected!
True
>>> m.run_code('a=1;')
{'content': {'stdout': '', 'datadir': '/private/tmp/MatlabData/', 'code': 'a=1;', 'figures': []}, 'success': 'true'}
{'content': {'stdout': '', 'datadir': '/private/tmp/MatlabData/', 'code': 'a=1;', 'figures': []}, 'success': True}
>>> m.get_variable('a')
1

Expand Down Expand Up @@ -260,7 +260,7 @@ def is_connected(self):
def is_function_processor_working(self):
result = self.run_func('%s/usrprog/test_sum.m' % MATLAB_FOLDER,
{'echo': '%s: Function processor is working!' % self._program_name()})
return result['success'] == 'true'
return result['success']

def _json_response(self, **kwargs):
return json.loads(self._response(**kwargs), object_hook=decode_pymat)
Expand Down Expand Up @@ -288,9 +288,15 @@ def run_func(self, func_path, *func_args, **kwargs):
nargout = kwargs.pop('nargout', 1)
func_args += tuple(item for pair in zip(kwargs.keys(), kwargs.values())
for item in pair)
return self._json_response(cmd='run_function',
func_path=func_path,
func_args=func_args,
dname = os.path.dirname(func_path)
fname = os.path.basename(func_path)
func_name, ext = os.path.splitext(fname)
if ext and not ext == '.m':
raise TypeError('Need to give path to .m file')
return self._json_response(cmd='eval',
func_name=func_name,
func_args=func_args or '',
dname=dname,
nargout=nargout)

def run_code(self, code):
Expand All @@ -301,17 +307,16 @@ def run_code(self, code):
code : str
Code to send for evaluation.
"""
return self._json_response(cmd='run_code', code=code)
return self.run_func('evalin', 'base', code, nargout=0)

def get_variable(self, varname, default=None):
response = self._json_response(cmd='get_var', varname=varname)
return response['var'] if response['exists'] else default
resp = self.run_func('evalin', 'base', varname)
return resp['result'] if resp['success'] else default

def set_variable(self, varname, value):
if isinstance(value, spmatrix):
return self._set_sparse_variable(varname, value)
return self.run_func('pymat_set_variable.m',
{'name': varname, 'value': value})
return self.run_func('assignin', 'base', varname, value, nargout=0)

def set_default_plot_size(self, width=512, height=384):
code = "set(0, 'defaultfigurepaperunits', 'inches');\n"
Expand Down
4 changes: 3 additions & 1 deletion pymatbridge/tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ def test_create_func(self):

def test_pass_kwargs(self):
resp = self.mlab.run_func('plot', [1, 2, 3], Linewidth=3)
assert resp['success'] == 'true'
assert resp['success']
assert len(resp['content']['figures'])
resp = self.mlab.plot([1, 2, 3], Linewidth=3)
assert resp['result'] is not None
assert len(resp['content']['figures'])
2 changes: 1 addition & 1 deletion pymatbridge/tests/test_run_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def test_undefined_code(self):
success = self.mlab.run_code("this_is_nonsense")['success']
message = self.mlab.run_code("this_is_nonsense")['content']['stdout']

npt.assert_equal(success, "false")
assert not success
if tu.on_octave():
npt.assert_equal(message, "'this_is_nonsense' undefined near line 1 column 1")
else:
Expand Down