1

I can call and pass parameters to Matlab using Python like so:

    MatlabExePth=checkMatlabExe()
    matlab_cmd_string = MatlabExePth+ " -nosplash -nodesktop -wait -r "
    changeDirectory("C:\\SVN\\Matlabcode")
    mat_cmd = matlab_cmd_string + "\"" + 'testMatlabReturnToPython' +  ", exit\""
    # test return value Method 1
    msg=subprocess.check_output(mat_cmd,stderr=subprocess.STDOUT,shell=True)
    # test return value method 2
     proc = subprocess.Popen(cmd_string, stdout=subprocess.PIPE, shell=True)
     out, err = proc.communicate()

where testMatlabReturnToPython.m code looks like:

    function [err,err_cod] = testMatlabReturnToPython()
      try

        mse=0.01;
        thr=0.1;
        if(mse>thr)
            err = 'no error'
            err_cod = 0;
        else
            % cause an exception
            [1 2]*[1 2];
        end
     catch
         err = ' error'
         err_cod = -1;
     end

As with Python code, I need a way to get back the err and err_code variables back into Python (these could be arrays, cell var etc), but I haven't found a way to do so.

Note: After Calling Matlab support, found out that import matlab.engine will do the above but is available from matlab R2014b, while i have R2013b.

user915783
  • 689
  • 1
  • 9
  • 27
  • I think you have to further explain what you are doing? Calling Matlab via command line? Or via some other API? – Daniel Sep 17 '15 at 11:26
  • @Daniel calling Matlab from Python. Its related to : [link](http://stackoverflow.com/questions/32598006/returning-gracefully-from-matlab-back-to-python)[link] – user915783 Sep 17 '15 at 16:23
  • A specific reason you use command line and not Matlab automation you can access via win32com.client in python? – Daniel Sep 17 '15 at 18:21
  • 1
    http://stackoverflow.com/questions/2883189/calling-matlab-functions-from-python some details about the possibilities – Daniel Sep 17 '15 at 18:24
  • @Daniel Thanks Daniel, that was an excellent resource. – user915783 Sep 17 '15 at 19:25

1 Answers1

1

How about that small following example with try-catch?

function [my_out, status] = myfun(my_inputs)
  try
    do_your_work_here;
    my_out = your_result;
    status = 1;
  catch ME
    my_out = [];
    status = -1;
    warning(ME.getReport());
  end
end

UPDATE: Regarding to your updated question, to get values from matlab function in python, you may need something similar to this in python:

import matlab.engine as ME
PE = ME.start_matlab()
err,err_cod = PE.testMatlabReturnToPython(nargout=2)

sorry that i don't have python now so cannot confirm if it runs perfectly.

scmg
  • 1,904
  • 1
  • 15
  • 24
  • seems you want to call matlab function from python? or am i misunderstanding? – scmg Sep 17 '15 at 17:24
  • yes I am able to call and pass arguments but not able to read values back – user915783 Sep 17 '15 at 17:26
  • updated the answer, you may refer to http://www.mathworks.com/help/matlab/apiref/matlab.engine.matlabengine-class.html for more information – scmg Sep 17 '15 at 17:35
  • then you should update that information, mention clearly which license you don't have, in your question's title, hope others can help – scmg Sep 17 '15 at 17:43