English 中文(简体)
使用系统调用从MATLAB执行python脚本时出现的问题
原标题:Problem with using system call to execute python script from MATLAB

我有一个MATLAB函数,它需要与python代码进行通信(不快速,也不经常)。我用MATLAB把数字写入一个文件。Python读取文件,进行一些计算,并将一些结果写入另一个文件。然后MATLAB读取该文件并继续前进。

我遇到的问题是当我想从MATLAB中执行python脚本时。我过去发现,在MATLAB中简单地执行系统调用就足够了:

系统(python-myscript.py)

然而,当numpy函数loadtxt在python脚本中达到这一点时,它似乎并不喜欢:

名称错误:未定义名称loadtxt

我正在定义它(从numpy导入*)。如果我只是从终端执行脚本,它会使用loadtxt读取文件。只有当我使用系统调用执行脚本时,我才会出现错误。python.py文件与进行系统调用的MATLAB.m文件位于同一目录中。我已经用同样的方法在没有loadtxt的情况下执行了其他脚本。

有什么建议吗?

最佳回答

看起来你在使用OS X。非常建议不要试图在OS X中使用python解释器的全局定义。

你可以直接打电话给想要的口译员。因此,与其调用system(python-myscript.py),不如获取所需解释器进入变量的完整路径并调用:

system([use_this_python   myscript.py ])

或者类似的东西。

确切地说,你想如何确定要使用什么python解释器是由你和使用它的人决定的。也许默认情况下只调用“python”,但允许用户通过配置文件指定一些任意的解释器。

问题回答

系统上的shell和执行system命令时由Matlab打开的shell不一定相同。

重要的是,可能存在不同的环境变量。例如,尝试执行系统(echo$PATH)来检查您是否可以访问所有库。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签