Operation and maintenance series (12) -- MATLAB command line calls python, python reads file parameter calculation results and accesses files, MATLAB reads file results

MATLAB command line calls python, python reads the file parameter calculation result accesses the file, MATLAB reads the file result

Matlab outputs all parameters to a file, and then uses the system command to call the python script. The python script reads the file and does the calculation result and then writes the file. Finally, matlab reads the file to get the result.

Suppose the usage of the python script is:

python pythontest.py in.txt out.txt

pythontest.py code:

# -*- 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()

Then the MATLAB call command is:

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325599885&siteId=291194637