Running a matlab function with python

조회 수: 11 (최근 30일)
jacopo tacconi
jacopo tacconi 2016년 4월 23일
댓글: Gautam Mohan 2016년 4월 27일
Dear all, I am executing a Matlab function that I have created from python by means of the Matlab engine, however I get the following error:Integers can only be raised to positive integral powers. Running the function from Matlab does not give any problem, therefore I do not understand what is wrong when I do it from python. Do you have any idea of the cause of this problem? Thanks.

채택된 답변

Bo Li
Bo Li 2016년 4월 25일
Not sure what exact MATLAB function you are calling from Python, but it looks like that you are passing a Python integer to MATLAB while the MATLAB function expects a non-integer as input.
For example, following function call in Python gives you error "Undefined function 'sqrt' for input arguments of type 'int64'.":
>>eng.sqrt(4)
The reason is that Python integer is converted into MATLAB int64, but "sqrt" does not work with int64. However, "sqrt(4)" works in MATLAB because by default the type of a number is double in MATLAB. You could see the same error if you run "sqrt(int64(4))" in MATLAB.
More related info:
  댓글 수: 2
jacopo tacconi
jacopo tacconi 2016년 4월 25일
Hi Bo Li, thanks for your answer. At the end, I discovered by myself the problem and yes it was exactly how you have said. I solved by giving to Matlab the input as float. At the beginning I believed that even if I was launching Matlab by python, the program automatically changed the integer variable in floating without any problem. Anyway, thank you very much for the help.
Gautam Mohan
Gautam Mohan 2016년 4월 27일
As an additional note, here is some more information on handling data types when dealing with Python and MATLAB:
It's best to ensure values passed to MATLAB are doubles, as this will allow the most flexibility and accuracy with computing due to MATLAB's behavior of using doubles as defaults. Integers and other data types should only be used in application-specific cases.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by