我有用户Matlab函数。这是别人写的。该文件中有多个函数,但第一个与.m文件同名的函数是我试图调用的函数。它有4个参数(字符串、字符串、字符串和布尔值(。通常在Matlab中,我这样称呼它:函数('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)
而且效果非常好。谢谢