-1

I want to run and access the output of a Python function in Matlab. Please find below function. The python function returns a Python tuple as output in Matlab. Can I access elements of tuple in Matlab? I do not want to export output as .mat file and import it in Matlab, which will be computationally expensive for my work. Thanks

Python code: python_file name: test_file

import numpy as np
def test (x1, x2, x3, x4):
    y = x1 + x2 + x3 + x4
    z = y**2   
    return {"y_value":y,"z_value": z}

Calling python function in Matlab:

f = py.test_file.test(2.2,3.9,4.2,5.1)

Output received in Matlab as of 1 by 1 tuple.

f = 

  Python tuple with no properties.

    (15.4, 237.16000000000003)
Husnain
  • 243
  • 1
  • 2
  • 5
  • 1
    The documentation is quite clear: https://www.mathworks.com/help/matlab/matlab_external/pythontuplevariables.html It was the top hit when I googled for “matlab python tuple”. – Cris Luengo Dec 27 '22 at 14:13

1 Answers1

0

After you got

f = 

  Python tuple with no properties.

    (15.4, 237.16000000000003)

Use

cellf=cell(f);

to convert it to matlab cells. Optionally, you may index into the tuple first:

cellf12=cell(f(1:2));
X Zhang
  • 1,081
  • 7
  • 17