3

I have installed "MATLAB engine for Python" from MathWork. My python version is 3.4 and it works well if I put my own-written m.file together with .py file under the same folder.

But everything went wrong when I want to use that engine in Django. The .m function is called "doa.m". The structure of my project is like this: Project Structure

and my views.py is:

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')

and my urls.py is:

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

However, when I go into http://127.0.0.1:8000/result/, the error is: Undefined function or variable 'doa'. Trceback is:

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'.

Can anyone explain it to me? Thanks!

miata
  • 81
  • 6
  • 2
    Instead of your own file, could your try to call some built-in command? Maybe `eng.plus(1,2,nargout=1)`? Did you add your own m file to the search path in matlab? Looks like the engine is okay but matlab isn't finding your file. – Daniel Feb 14 '16 at 12:29
  • 1
    @Daniel Hi, I tried b=eng.max(100,200) and it worls fine. – miata Feb 14 '16 at 12:44
  • 2
    Check this out: http://stackoverflow.com/questions/35330659/matlab-script-not-found-from-django-views-py/35331155 – Selcuk Feb 14 '16 at 13:03

1 Answers1

0

For example, for Matlab 2015b, under "Home" Tab, click "set path", then click "add folder" and add the folder path where your Django project exists.

miata
  • 81
  • 6