运维系列(12)-- MATLAB命令行调用python,python读取文件参数计算结果存取文件,MATLAB读取文件结果

MATLAB命令行调用python,python读取文件参数计算结果存取文件,MATLAB读取文件结果

matlab把所有参数输出到一个文件里,然后用system命令调python脚本。python脚本读文件做计算结果再写文件。最后matlab再读文件得到结果。

假设python脚本的用法是:

python pythontest.py in.txt out.txt

pythontest.py代码:

# -*- coding: utf-8 -*-
import sys
if __name__=="__main__":
        infile = sys.argv[1]
        outfile = sys.argv[2]
        fin = open(infile, 'r')
        fout = open(outfile, 'w')
        a = fin.readline().strip()
        b = fin.readline().strip()
        c = float(a)+float(b)
        fout.write('%f' % c)
        fout.close()
        fin.close()

则MATLAB调用命令为:

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

猜你喜欢

转载自blog.csdn.net/dooonald/article/details/79841847
今日推荐