Function generation error using python code in matlab

36 ビュー (過去 30 日間)
성현
성현 2025 年 11 月 12 日 8:52
コメント済み: 성현 2025 年 11 月 19 日 0:40
%% download sympy module
pe = pyenv;
exe = string(pe.Executable);
cmd1 = '"' + exe + '"' + " -m pip install --upgrade pip";
cmd2 = '"' + exe + '"' + " -m pip install sympy";
system(cmd1);
system(cmd2);
%% function generation
sp = py.importlib.import_module('sympy');
x = sp.symbols('x');
f = sp.Function('f');
y = f(x); % <-- error here
eq = sp.Eq(sp.diff(y,x,2)+3*y-1,0);
Hi, all.
I'm trying to make function with python code in matlab m-file.
But, when it carry on 'y=f(x)' code, make a error
'Python object is neither string nor unicode. Cannot return chars.'
and there is no object 'y' in workspace.
What should I edit to run this code properly.
Thanks.

採用された回答

Leepakshi
Leepakshi 2025 年 11 月 17 日 11:50
Hey,
When you call f(x), the result is a SymPy object, not a string or number. MATLAB cannot convert this to a native MATLAB type, so you must work with it as a Python object in MATLAB. Try converting it into MATLAB compatible datatype.
Thanks
  1 件のコメント
성현
성현 2025 年 11 月 19 日 0:40
Thank you for letting me know.

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

その他の回答 (0 件)

カテゴリ

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