在 Python 中使用 Matlab 用户函数会出错

Using a Matlab user function in Python gives an error

我有用户 Matlab 函数。是别人写的。该文件中有多个函数,但第一个与 .m 文件同名的函数是我尝试调用的函数。它有 4 个参数(字符串、字符串、字符串、布尔值)。 通常在 Matlab 中我这样称呼它: function('string', 'string, true) 在 Python 中尝试时,我使用以下代码。

def CodeGenerationAndResim(self, Models=None, Resim=False, Type='TW'):
    """ This function will start Matlab and generates code for each Simulink model
        and creates Resim archives for the release.
        All variables:
            Models                      - All Sim Modelto generate c code for
            Resim                       - Running Resim function
            Type                        - Type of Resim archive
            matlabEngine                - Matlab engine to start Matlab
            MatlabEngineSuccessfull     - Checks if Matlab engine is started without error
            CodeGenSuccessfull          - Checks if Code Generation is successsful
            ResimSuccessfull            - Checks if Resim function is performed successfully.
    """


    # Check variables
    MatlabEngineSuccessfull = True
    CodeGenSuccessfull = True
    ResimSuccessfull = True


    # Call Matlab engine
    try:
        matlabEngine = matlab.engine.start_matlab('-nodesktop', background = False)
        matlabEngine.addpath("C:\temp")
    except EngineError as e:
        MatlabEngineSuccessfull = False
        Print("Could not start Matlab. Do you have Matlab 64 bits installed?")

    # Generate code for models in a loop
    if Models is not None and MatlabEngineSuccessfull:
        for model in Models:
            try:
                matlabEngine.rtwbuild(model)
            except Exception as e:
                CodeGenSuccessfull = False
                print('Something went wrong')

    # Running Resim
    if Resim and MatlabEngineSuccessfull and CodeGenSuccessfull:
        try:
            matlabEngine.resimPrepareNewVersion('all','DAS2_TwYYWW_2005',1)
        except Exception as e:
            ResimSuccessfull = False
            print(e)


    return MatlabEngineSuccessfull, CodeGenSuccessfull, ResimSuccessfull

我得到的错误如下:

Error using resimPrepareNewVersion
Too many output arguments.
Too many output arguments.

我不明白这个错误。我已经给出了与 Matlab 中相同的论点。即使我给它 3 个、2 个或没有参数,我也会得到同样的错误。当 Matlab 加载时,这个函数会自动加载,所以我不需要添加路径,但我还是这样做了。但是,我尝试没有给出任何路径。代码生成和启动 Matlab 没有任何问题。在 Matlab 中,此函数加载一些其他文件并打开一个信息对话框,它意味着任何东西。

奇怪的是,我总是将错误标记为 Too many input arguments 而不是 output arguments

我通过插入 nargout=0 解决了这个问题。

已编辑:

matlabEngine.resimPrepareNewVersion('all','DAS2_TwYYWW_2005',1)

收件人:

matlabEngine.resimPrepareNewVersion('all','DAS2_TwYYWW_2005',1, nargout=0)

而且效果很好。谢谢。