如何在 Django 中调用 "MATLAB engine for Python"?

How to call "MATLAB engine for Python" in Django?

我已经从 MathWork 安装了 "MATLAB engine for Python"。我的python版本是3.4,如果我把自己写的m.file和.py文件放在同一个文件夹下就可以了。

但是当我想在 Django 中使用该引擎时,一切都出错了。 .m 函数称为 "doa.m"。我的项目结构是这样的: Project Structure

我的 views.py 是:

import os
....
from django.shortcuts import render
import matlab.engine
....
def result(request):
    eng = matlab.engine.start_matlab()
    eng.doa(nargout=0)
    return render(request, 'result.html')

我的 urls.py 是:

url( r'result/$', views.result, name='result'),

但是,当我进入 http://127.0.0.1:8000/result/ 时,错误是: 未定义函数或变量 'doa'。 回溯是:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/result/

Django Version: 1.9.2
Python Version: 3.4.4
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'photos',
 'jfu']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "J:\Program Files\Python34\lib\site-packages\django\core\handlers\base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "J:\Program Files\Python34\lib\site-packages\django\core\handlers\base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "I:\Eclipse_Workspace\demo\photos\views.py" in result
  60.     eng.doa(nargout=0)

File "J:\Program Files\Python34\lib\site-packages\matlab\engine\matlabengine.py" in __call__
  84.                                 _stderr).result()

File "J:\Program Files\Python34\lib\site-packages\matlab\engine\futureresult.py" in result
  106.                 self._future,self._nargout, None, out=self._out, err=self._err)

Exception Type: MatlabExecutionError at /result/
Exception Value: Undefined function or variable 'doa'.

谁能给我解释一下?谢谢!

例如,对于 Matlab 2015b,在 "Home" 选项卡下,单击 "set path",然后单击 "add folder" 并添加您的 Django 项目所在的文件夹路径。