colon operator in compiled python package

2 ビュー (過去 30 日間)
Jieqiang Wei
Jieqiang Wei 2020 年 11 月 9 日
コメント済み: Sean de Wolski 2020 年 11 月 12 日
Hi, I compiled one python package from a matlab one using library compiler. Everything went well until I did a test in Python’s Interpreter. In my Matlab code, I have a function called returnS_test_compiler with a line
f = (fRange(1):fRange(2):fRange(3))';
which generates a list and then "transpose" it.
I assume by compiling the code into python package, I do not need to modify anything in the code. So when I call this returnS_test_compiler in python imported as a python package, it gives the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/MATLAB/MATLAB_Runtime/v97/toolbox/compiler_sdk/pysdk_py/matlab_pysdk/runtime/deployablefunc.py", line 80, in __call__
nlhsWasSpecified, stdoutObj, stderrObj).result()
File "/usr/local/MATLAB/MATLAB_Runtime/v97/toolbox/compiler_sdk/pysdk_py/matlab_pysdk/runtime/futureresult.py", line 135, in result
raise e
File "/usr/local/MATLAB/MATLAB_Runtime/v97/toolbox/compiler_sdk/pysdk_py/matlab_pysdk/runtime/futureresult.py", line 123, in result
raise e
File "/usr/local/MATLAB/MATLAB_Runtime/v97/toolbox/compiler_sdk/pysdk_py/matlab_pysdk/runtime/futureresult.py", line 113, in result
self._nlhs, out=self._out, err=self._err)
matlab_pysdk.runtime.MatlabRuntimeError: An error occurred when evaluating the result from a function. Details:
File /root/.mcrCache9.7/return0/returnS_test/returnS_test_compiler.m, line 15, in returnS_test_compiler
Undefined function 'colon' for input arguments of type 'cell'.
It seems that it is complaining ( : : ) is not recognisable as a python sentence. Any suggestions?

回答 (3 件)

Steven Lord
Steven Lord 2020 年 11 月 9 日
The variable fRange is a cell array. The colon operator doesn't know how to work on cell arrays.
a = {1};
b = {10};
x = a:b % throws error
Operator ':' is not supported for operands of type 'cell'.
To get this to work, pass the contents of the cells in the cell arrays to the colon operator.
a = {1};
b = {10};
x = a{1}:b{1} % does not throw an error.

Sean de Wolski
Sean de Wolski 2020 年 11 月 9 日
You should call the MATLAB module with a numeric input rather than a list. This is the data type conversion table. Use numeric types (probably matlab.double)
https://www.mathworks.com/help/releases/R2020b/matlab/matlab_external/pass-data-to-matlab-from-python.html
  1 件のコメント
Sean de Wolski
Sean de Wolski 2020 年 11 月 12 日
This page is even more clear in exactly what you need to do:
https://www.mathworks.com/help/releases/R2020b/matlab/matlab_external/matlab-arrays-as-python-variables.html

サインインしてコメントする。


Jieqiang Wei
Jieqiang Wei 2020 年 11 月 10 日
I find this one is closest to what I am looking for, it seems the way I define the input variable in python manner directly is not a good option.
https://se.mathworks.com/matlabcentral/answers/437509-python-interface-to-matlab-runtime#comment_656434
  1 件のコメント
Sean de Wolski
Sean de Wolski 2020 年 11 月 12 日
You seem to be finding the engine so that doesn't seem like the issue. You just need to cast the python data to a matlab double. The doc page I linked shows how to do this.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCall Python from MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by