3
def c1(a1,b1):
    a1=2
    b1=3
    cc=a1+b1
    return cc

I have saved this function in test.py. When I use this function in MATLAB I encountered this problem:

import py.test.* c1(2,3)
Undefined function 'c1' for input arguments
of type 'double'.
  py.test.c1(2,3)
Undefined variable "py" or class
"py.test.c1".

How can I use .py function in MATLAB R2014b?

0

2 Answers 2

3

If you get the error message below, a failure has occurred.

Undefined variable "py" or class

There are a lot of things that might be wrong here, and Mathworks have actually set up a whole tutorial for how to troubleshoot this problem. (The title of the page is actually: Undefined variable "py" or function "py.command", so it should contain most of what you need)

Check out the following:

  • Python Not Installed
  • 64-bit/32-bit Versions of Python on Windows Platforms
  • MATLAB Cannot Find Python
  • Error in User-Defined Python Module
  • Python Module Not on Python Search Path
  • Module Name Conflicts
  • Python Tries to Execute command in Wrong Module
Sign up to request clarification or add additional context in comments.

Comments

1

Starting from Matlab 2014b Python functions can be called directly - using prefix py, then module name, and finally function name like so:

result = py.module_name.function_name(parameter1);

However, one has to make sure to add the script to the Python search path when calling from Matlab (especially if the first time Python is called the current working directory is different than that of the Python script.

See more details here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.