Can I call my own custom Matlab functions from python (R2014b)?

I've just installed R2014b and am hoping to use the new MATLAB Engine for Python capabilities.
I can successfully call standard functions like isprime from python as shown in the documentation.
Is it possible for me to call my own custom Matlab functions from python using the Engine?

 採用された回答

Bo Li
Bo Li 2014 年 12 月 10 日

4 投票

Yes, you can also call your own custom MATLAB function from Python Engine. However, you need to add the folder of your own MATLAB file (for example c:\demo\foo.m) into MATLAB path:
>>>eng.addpath(r'c:\demo',nargout=0)
>>>eng.foo()
or change to the directory where your MATLAB file is located:
>>>eng.cd(r'c:\demo')
>>>eng.foo()
If you already have the folder of your MATLAB file in the search path, you can call it directly from Python:
>>>eng.foo()

3 件のコメント

Claire
Claire 2014 年 12 月 11 日
Thanks Bo Li. Just verified your code myself with a simple function myself, all that needs to be done is add the folder to the matlab path as you suggested.
Also thank Mathworks for adding this python engine! I have a feeling its going to be quite useful for me!
Joshua Bone
Joshua Bone 2020 年 6 月 17 日
What is the purpouse of the "nargout" argument here?
Tom Sloan
Tom Sloan 2020 年 8 月 10 日
@Joshua Bone Its so the engine will know how many variables to expect on the return of the function. You'll find several functions require this (such as the save func). I am not sure why all don't require this. Here's a link to the nargout docs

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

その他の回答 (0 件)

カテゴリ

質問済み:

2014 年 12 月 10 日

コメント済み:

2020 年 8 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by