0

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.

Community
  • 1
  • 1
gaya
  • 463
  • 2
  • 6
  • 22
  • 1
    Why are you using `matlab.run` and not `matlab.foo`? `matlab.run` is going to execute [`run`](https://www.mathworks.com/help/matlab/ref/run.html), which only accepts one input argument. It's also been years since mlab was last updated, perhaps you should give [MATLAB's native Python bridge](https://www.mathworks.com/help/matlab/matlab-engine-for-python.html) a try. – sco1 Sep 06 '17 at 12:48
  • I tried `matlab.foo` now, again no output. And no error as well. As for python bridge, I do not have admin rights and to install something I need to contact IT and so on which is sadly not an option right now. – gaya Sep 06 '17 at 12:53
  • As an aside, please never use built-in function names as variable names (in any language). In this case, don't use `sum` as a variable in MATLAB. This is likely not your actual issue, but could cause you one down the line by confusing yourself and/or your compiler! – Wolfie Sep 06 '17 at 12:59
  • @Wolfie noted :) – gaya Sep 06 '17 at 13:02

0 Answers0