Running a Python Script with multiple inputs and outputs through Matlab
Mostrar comentarios más antiguos
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
Más respuestas (2)
Rhea Chandy
el 23 de Ag. de 2021
1 voto
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
Constantinos Florides
el 28 de Ag. de 2021
Dennis Kachila
el 23 de Nov. de 2024
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.
- 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
Más información sobre Call Python from MATLAB en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!