How to pass Matlab variables to pyrunfile?

14 次查看(过去 30 天)
My Matlab script:
clc
clear all
close all
% Define Matlab variable
var01 = 'Some string';
pyrunfile("pyScript.py", var01)
The pyScript.py:
import sys
arg01 = sys.argv[1]
print(arg01)
The error I'm getting when I run it:
Error using <string>><module>
Python Error: IndexError: list index out of range
What would be the proper way to pass Matlab variable to a python script that can be assigned with sys.argv[1]?
Thank you,

采纳的回答

Angelo Yeo
Angelo Yeo 2023-7-3
clc
clear all
close all
% Define Matlab variable
var01 = 'Some string';
pyrunfile("pyScript.py '" + var01 + "'")
FYI, you can refer to the official doc for pyrunfile.
  2 个评论
PeterW
PeterW 2023-7-5
Thank you Angelo. I read the doc but for some reason I couldn't figure out how it will work for my case.
Your solution works.
Thanks again.
Angelo Yeo
Angelo Yeo 2023-7-5
I am happy that the solution worked for you 😊 Another reason why I left the doc link is that someone else can see this post later and refer to the official doc. Have a good day!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Call Python from MATLAB 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by