I have a problem with running matlab code in python. When I run my easy code, I have this information in matlab console:
Undefined function or variable 'messenger'.
Error in matlabserver (line 7) messenger('init', socket_address);
Matlab code:
%% MATLAB
function lol = yourfunc(args)
arg1 = args.arg1;
arg2 = args.arg2;
lol = arg1 + arg2;
end
Python code:
from pymatbridge import Matlab
mlab = Matlab()
mlab.start()
res = mlab.run('...path\yourfunc.m', {'arg1': 3, 'arg2': 5})
print(res['result'])
-
Hi, are you running this on 32 bit linux machine?WoodyDev– WoodyDev2018年02月02日 10:06:01 +00:00Commented Feb 2, 2018 at 10:06
-
I running it on windows 10 64 and pycharmDzejkob28– Dzejkob282018年02月02日 12:14:26 +00:00Commented Feb 2, 2018 at 12:14
-
Looking at this github thread @quic0 : I had the same problem on Windows 10. The problem is that Matlab cannot find the mex file messenger.mexw64. It connects if you copy the mex file from pymatbridge\messenger\mexw64 to pymatbridge\matlab in your Python package folder. It seems that all other issues relate to re-compiling the messenger mex file specifically for your systemWoodyDev– WoodyDev2018年02月02日 12:20:19 +00:00Commented Feb 2, 2018 at 12:20
-
Ok, now it's works, but i have a problem, when i print 'res' in pycharm, i have this "{'result': '', 'success': False, 'content': {'stdout': 'Too many input arguments.'}}"Dzejkob28– Dzejkob282018年02月02日 12:51:03 +00:00Commented Feb 2, 2018 at 12:51
-
Dzejkob, congrats on your first answered question and welcome to the site. Please have a read at the site's guidlines on how to ask questions. Specifically, avoid lumping a second question on the original one (especially since the two are essentially completely unrelated, and the second question no longer corresponds to the question body), but prefer to ask such questions as separate standalone questions. Also, consider using more informative titles that would help future users googling for the same error. "Matlab error" is incredibly uninformative.Tasos Papastylianou– Tasos Papastylianou2018年02月02日 14:17:03 +00:00Commented Feb 2, 2018 at 14:17
1 Answer 1
So as a dedicated answer to the OP question (initially it was necessary to re-compile the mex file messenger.mexw64 for windows 10 [see the original post comments]):
Ok, now it's works, but i have a problem, when i print 'res' in pycharm, i have this "{'result': '', 'success': False, 'content': {'stdout': 'Too many input arguments.'}}"
In your matlab function you stated only one argument:
function lol = yourfunc(args)
But when you call it in python you are calling it with two input arguments (arg1 & arg2):
res = mlab.run('...path\yourfunc.m', {'arg1': 3, 'arg2': 5})
In the matlab function input you are assuming it is a data structure with at least the two fields arg1 & arg2; you are referencing these variables from the structure when you call
args.arg1
args.arg2
As I am not sure if you can pass a data structure this technique (as a JSON style string input from a python function) I would propose changing the input arguments to the Matlab function to either:
Change your function to support two arguments like how you are calling it from python:
%% MATLAB function lol = yourfunc(input1,input2) arg1 = input1; arg2 = input2; lol = arg1 + arg2; end # PYTHON (just to be clear) from pymatbridge import Matlab mlab = Matlab() mlab.start() res = mlab.run('...path\yourfunc.m', {'input1': 3, 'input2': 5}) print(res['result'])Or if you want to handle an infinite number of inputs (using varargin):
%% MATLAB function lol = yourfunc(varargin) arg1 = varargin{1}; arg2 = varargin{2}; lol = arg1 + arg2; end # PYTHON (just to be clear) from pymatbridge import Matlab mlab = Matlab() mlab.start() res = mlab.run('...path\yourfunc.m', {'input1': 3, 'input2': 5}) print(res['result'])
This should then remove the error message about too many input arguments and run successfully :)