Running a Python Script with multiple inputs and outputs through Matlab

Hallo, i have a pytho script with 10 inputs and 5 outputs and the on of the outputs is a list. I want to run this script throught matlab and give my inputs in matlab and then receive the outputs from Python in Matlab. Is that possible? I have tried some things, but i could redirect only one output from python to matlab. I am open to suggestion :)

 Respuesta aceptada

One interesting trick is to use json. From python, serialize all the outputs into one single json string. In matlab, convert the json string back to json.

2 comentarios

That is a very interesting idea!Is there a reason to pass from python a json string and not direct make a json file?
Yes, you can do it with a json file as well. But then you need to write and read the file. Also if you write to a file, it doesn't need to be json.

Iniciar sesión para comentar.

Más respuestas (2)

Hello Constantinos,
I understand that you're trying to run python script in MATLAB and then recieve outputs from Python in MATLAB.
Along with the above answer, you can also take a look at this previously answered question: Running python script in matlab
You will find the following resources with more information and examples here:

1 comentario

Yeap, i am trying to run a python script throught Matlab, i have seen these topics. They were very useful. The only part that i need to solve is to pass my outputs with json file to matlab:)

Iniciar sesión para comentar.

Yes, it is possible to run a Python script with multiple inputs and outputs in MATLAB. One efficient way to achieve this is by using JSON serialization in Python to bundle all outputs into a single JSON string as suggested by @Yongjian Feng. MATLAB can then decode this string into a struct for easy access to all outputs, including lists.
Below is an example implementation for a Python script with two inputs and two outputs, one of which is a list, and the corresponding MATLAB code to interface with it.
  1. Python Script Example (sqrt_pwr.py)
This Python script calculates the square root and raises a number to a specified power. The results are serialized into a JSON string.
import math
import json
#Calculate the square root of a number
def calculate_sqrt(x):
return math.sqrt(x)
#Function to raise a number to a specific power
def power_list(x, p):
return [x**i for i in range(1, p+1)] # List of powers up to p
#Input from MATLAB
print('Perfoming calculations')
result_sqrt = calculate_sqrt(input_value) # Square root
result_powers = power_list(input_value, pow_n) # List of powers
#Serialize the results into a JSON string
results = {
"result_sqrt": result_sqrt,
"result_powers": result_powers
}
#Output as a JSON string
output_json = json.dumps(results)
2. MATLAB Code to Call the Python Script
The MATLAB script passes inputs to the Python script and retrieves all outputs as a single JSON string, which is then parsed into a MATLAB struct.
% Check Python Environment
pe = pyenv;
disp(pe)
% Example where the we have more than one input and output
% Specify the input value for the calculations
num = 25;
power_num = 2;
% Pass input_value to the Python script and retrieve the JSON string
output_json = pyrunfile("sqrt_script.py", "output_json", input_value=num, pow_n=power_num );
% Convert the Python string to a MATLAB string
output_json_matlab = string(output_json); % Or char(output_json)
% Decode the JSON string into a MATLAB struct
results = jsondecode(output_json_matlab);
% Access individual results from the struct
result_matlab_sqrt = results.result_sqrt; % Square root
result_matlab_power = results.result_power; % Power
% Display the results
disp(['The square root of ', num2str(num), ' is: ', num2str(result_matlab_sqrt)]);
disp(['The power of ', num2str(num), ' squared is: ', num2str(result_matlab_power)]);

Categorías

Productos

Versión

R2020a

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by