Running a Python Script with multiple inputs and outputs through Matlab
조회 수: 10 (최근 30일)
이전 댓글 표시
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 :)
댓글 수: 0
채택된 답변
Yongjian Feng
2021년 8월 21일
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
Yongjian Feng
2021년 8월 29일
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.
추가 답변 (2개)
Rhea Chandy
2021년 8월 23일
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:
Dennis Kachila
2024년 11월 23일
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)]);
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!