matlabpython-3.6matlab-engine

Using a Matlab user function in Python gives an error


I have user Matlab function. It is written by someone else. There are multiple functions in that file but the first function that has the same name as the .m file itsel is the one that I try to call. It gets 4 arguments (string, string, string, boolean). Usually in Matlab I call it like this: function('string', 'string, true) When trying in Python I use following code.

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

The error I get is following:

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

I don't understand the error. I already give the same arguments as in Matlab. Even when I give it 3, 2, or no arguments I get the same error. When Matlab loads this functions are loaded automatically so I don't need to add path but I did it any way. However, I tried without giving any path. Code generation and starting Matlab goes without any problem. In Matlab this function loads some other files and opens a info dialog it it means anything.


Solution

  • It was really strange that I all the time red the error as Too many input arguments instead of output arguments

    I solved the problem by inserting nargout=0.

    Edited:

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

    To :

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

    And it worked very well. Thanks.