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.

5
  • I think you have to further explain what you are doing? Calling Matlab via command line? Or via some other API? Commented Sep 17, 2015 at 11:26
  • @Daniel calling Matlab from Python. Its related to : link[link] Commented Sep 17, 2015 at 16:23
  • A specific reason you use command line and not Matlab automation you can access via win32com.client in python? Commented Sep 17, 2015 at 18:21
  • 1
    stackoverflow.com/questions/2883189/… some details about the possibilities Commented Sep 17, 2015 at 18:24
  • @Daniel Thanks Daniel, that was an excellent resource. Commented Sep 17, 2015 at 19:25

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

seems you want to call matlab function from python? or am i misunderstanding?
yes I am able to call and pass arguments but not able to read values back
updated the answer, you may refer to mathworks.com/help/matlab/apiref/… for more information
then you should update that information, mention clearly which license you don't have, in your question's title, hope others can help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.