I get an error when using numpy in my python script in simulink
显示 更早的评论
I try to use a python script (version 3.11 according to the pyenv command) as a function in simulink. I use python importer and it works fine when not using numpy. As soon as I start the script with 'import numpy as np' however, the blocks don't work anymore and I get the error:
MATLAB System block 'Mytester21/taketwo2_CreateArrayfivedim' error occurred when invoking 'setupImpl' method of 'taketwo2_CreateArrayfivedim'.
Caused by:
- Invalid HandleObjectRef - Entry not found in the server table
Component:Simulink | Category:Block error
The python script itself does not matter, as this issue arises with various different python files. Can anyone help me with this?
(an additional issue that I have is that I need to restart my computer every time I rework something in the python file, an update through obj.view does not update the simulink output.)
7 个评论
Bastiaan
2023-10-10
Rajat Kumar Raghav
2023-10-17
Hello Bastiaan,
I am also facing the same issue as mentioned above, but even with simple code for adding 2 numbers.
Please find the code below.
Can you please tell me how you fixed it.
def sum2ints(var1, var2):
sum_int = var1 + var2
print(sum_int)
return sum_int
if __name__ == '__main__':
num1 = int(input('Enter the number1: '))
num2 = int(input('Enter the number 2: '))
output = sum2ints(num1, num2)
print('Final output is : ', output)
Rajat Kumar Raghav
2023-10-17
Model and error looks like as shown in below image.

R
2023-11-14
Hi Rajat,
This error is possibly occurring due to python caching your script in memory when it is first loaded. This means that python is executing the old version of the function which is cached in sys.modules. Follow the steps in this link to reload your module and see the new result in Simulink:
Abhishek
2024-1-8
Hi Bastin ,
I also have the same issue. I freshly loaded a python script into SIMULINK and it is causing the following error.
=== Simulation (Elapsed: 2 sec) ===
Error:MATLAB System block 'untitled/temp_generatesquarepulse' error occurred when invoking 'setupImpl' method of 'temp_generatesquarepulse'.
Caused by:
Invalid HandleObjectRef - Entry not found in the server table
The python code works well as standalone and it has Numpy also used. For the old function to be cached, this was loaded the first time without any memory. So not sure how it works.
Abhishek
2024-1-26
Any idea why this error occurs ? I am still not able to solve this error for my simulink model when python code with numpy or other imports is imported using the python importer toolbox
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Call Python from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!