How to pass Matlab variables to pyrunfile?

14 Ansichten (letzte 30 Tage)
PeterW
PeterW am 3 Jul. 2023
Kommentiert: Angelo Yeo am 5 Jul. 2023
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,

Akzeptierte Antwort

Angelo Yeo
Angelo Yeo am 3 Jul. 2023
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 Kommentare
PeterW
PeterW am 5 Jul. 2023
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 am 5 Jul. 2023
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!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Call Python from MATLAB finden Sie in Help Center und File Exchange

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by