首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >子进程,打开Python

子进程,打开Python
EN

Stack Overflow用户
提问于 2017-06-10 07:13:52
回答 1查看 479关注 0票数 1

我想运行一个程序,比如说MATLAB或Python中的其他有限元分析软件,等待它运行并存储结果,然后在Python中再次使用以进一步处理。我找不到一个关于如何做到这一点的真正基本的例子。一个简单的代码或任何有用的链接将受到高度赞赏。help on Subprocess模块看起来有点复杂。

EN

回答 1

Stack Overflow用户

发布于 2018-11-29 21:59:45

我只是花了一段时间试图从令人沮丧的模糊的文档和示例中解决这个问题,并最终解决了这个问题。下面是一个非常简单的示例:

如何从运行MATLAB脚本(使用subprocess.Popen,无需安装matlab引擎)

MATLAB步骤1:创建您想要运行的脚本。在这个演示中,我有两个脚本,保存在文件夹C:/Users/User/Documents/ folder子过程中:

triangle_area.m

代码语言:javascript
复制
b = 5;
h = 3;
a = 0.5*(b.* h);  
save('a.txt','a', '-ASCII')

triangle_area_fun.m

代码语言:javascript
复制
function [a] = triangle_area(b,h)
a = 0.5*(b.* h);   %area
save('a.txt','a', '-ASCII')
end

步骤2:创建这两个.m文件后,下面的Python脚本使用subprocess.Popen()运行它们:

代码语言:javascript
复制
#Imports:
import subprocess as sp
import pandas as pd

#Set paths and options:
#note: paths need to have forward slashes not backslashes (why?!)
program  = 'C:/Program Files/MATLAB/R2017b/bin/matlab.exe'  #path to MATLAB exe
folder   = 'C:/Users/User/Documents/MATLABsubprocess'       #path to MATLAB folder with scripts to run
script   = 'triangle_area'                                  #name of script to run
options  = '-nosplash -nodesktop -wait'                     #optional: set run options  (nosplash? nodesktop means MATLAB won't open a new desktop window, wait means Python will wait until MATLAB is done beore continuing (needs to be paired with p.wait() after sp.Popen))
has_args = True                                             #set whether the MATLAB script needs arguments (i.e. is it a function?)

#Optional: define arguments to feed to function
if has_args ==True:
    script   = 'triangle_area_fun'                          #select script version with arguments
    b = 5 
    h = 3
    args = '({},{})'.format(b,h)                            #put all args into one string

#Set function string:
#Structure:  """path_to_exe optional_arguments -r "cd(fullfile('path_to_folder')), script_name, exit" """
#Example:    """C:/Program Files/MATLAB/R2017b/bin/matlab.exe -r "cd(fullfile('C:/Users/User/Documents/MATLABsubprocess')), triangle_area, exit" """
#basically, needs to know where the program to use lives, then takes some optional settings, -r runs the program, cd changes to the directory with the script, then needs the name of the script (possibly with arguments), then exits
fun =  """{} {} -r "cd(fullfile('{}')), {}, exit" """.format(program, options, folder, script)  #create function string that tells subprocess what to do
if has_args==True:
    fun = """{} {} -r "cd(fullfile('{}')), {}{}, exit" """.format(program, options, folder, script, args)
print('command:', fun)

#Run MATLAB:
print('running MATLAB script...')
p = sp.Popen(fun)                     #open the subprocess & run the MATLAB script 
p.wait()                              #wait until MATLAB is done before proceeding (this needs to be paired with -wait in options)
print('done')                         #if the run is successful, an output file named a.txt should appear in the folder with the MATLAB scripts

#Import MATLAB output files back into Python:
a = pd.read_csv('a.txt', header=None)             #read text file using pandas
print(a)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44467834

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档