matlab程序里調用python文件


用system調用:

參考連接:

https://zhidao.baidu.com/question/919137899588311499.html

https://blog.csdn.net/yanerhao/article/details/78669640

https://blog.csdn.net/u013685264/article/details/99596798

https://ww2.mathworks.cn/help/matlab/ref/system.html

matlab調用語句為:

[status, cmdout] = system('python xxx.py in.txt out.txt')

其中,xxx.py為要調用的文件,如果和當前的matlab文件不在同一目錄下,記得要把路徑補全;

in.txt 是xxx.py讀入的文件;

out.txt 是xxx.py輸出的文件。

即,matlab把所有參數輸出到in.txt文件里,然后用system命令調python腳本。python腳本讀in.txt文件做計算結果再寫文件到out.txt中。最后matlab再讀out.txt文件得到結果。

注意:

因為調用的python代碼用到了pytorch的庫。如果用上述語句運行的時候報錯,“import error: from torch._C import * RuntimeError: stoi”

改為:

[status, cmdout] = system('env -i /home/hwy/anaconda3/bin/python3 xxx.py');

就不會報錯了,如果沒有in.txt 和 out.txt 也可以不寫。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM