Reputation: 465
I have installed mlab for running a few .m scripts from python.
I have tested it code from this answer and it works very well. However my problem starts when I try to run a script using the run command.
Here is everything I have tried.
foo.m
function sum = foo(a,b)
sum= a+b;
fprintf ('sum', sum);
test.py
from mlab.releases import latest_release as matlab
sum= matlab.run('foo.m',5,6)
print sum
This gives the error
sum=matlab.run('foo.m',5,6)
File "D:\Program_Files\python\lib\site-packages\mlab-1.1.4
py2.7.egg\mlab\mlabwrap.py", line 607, in mlab_command
return self._do(name, *args, **update({'nout':nout}, kwargs))
File "D:\Program_Files\python\lib\site-packages\mlab-1.1.4-
py2.7.egg\mlab\mlabwrap.py", line 542, in _do
handle_out(mlabraw.eval(self._session, cmd))
File "D:\Program_Files\python\lib\site-packages\mlab-1.1.4-
py2.7.egg\mlab\mlabraw.py", line 67, in eval
matlab.eval(exp)
File "D:\Program_Files\python\lib\site-packages\mlab-1.1.4-
py2.7.egg\mlab\matlabcom.py", line 89, in eval
raise MatlabError(ret[begin:])
mlab.matlabcom.MatlabError: Error using run
Too many input arguments.
Then I tried to pass the arguments as a dictionary as here.
function sum = foo(args)
a= args.a
b= args.b
sum= a+b;
fprintf ('sum', sum);
In python,
sum=matlab.run('foo.m',{'a':5,'b':6})
print sum
resulting in error,
sum=matlab.run('foo.m',{'a':5,'b':6})
File "D:\Program_Files\python\lib\site-packages\mlab-1.1.4-
py2.7.egg\mlab\mlabwrap.py", line 607, in mlab_command
return self._do(name, *args, **update({'nout':nout}, kwargs))
File "D:\Program_Files\python\lib\site-packages\mlab-1.1.4-
py2.7.egg\mlab\mlabwrap.py", line 534, in _do
mlabraw.put(self._session, argnames[-1], arg)
File "D:\Program_Files\python\lib\site-packages\mlab-1.1.4-
py2.7.egg\mlab\mlabraw.py", line 76, in put
matlab.put({var_name : val})
File "D:\Program_Files\python\lib\site-packages\mlab-1.1.4-
py2.7.egg\mlab\matlabcom.py", line 128, in put
self.client.PutWorkspaceData(name, 'base', val)
File "<COMObject matlab.application>", line 3, in PutWorkspaceData
TypeError: Objects of type 'dict' can not be converted to a COM VARIANT
After I was really fed up and decided to run a simple file without any functions and arguments.
foo.m
clear all;
fprintf('This is python calling matlab');
test.py
matlab.run('foo.m')
This gives me no error but I do not see anything printed as well.
I do not know how to proceed now. I have tried to install mlabwrap as suggested in some answers but the installation itself doesn't work in windows. As mlab was just a repackaged version I thought it will work smoothly. I have thoroughly looked into all the questions here and nothing has helped me.
So basically how to run a .m file using mlab? With arguments even better.
Upvotes: 0
Views: 353