Index Error in python with matlab code

4 次查看(过去 30 天)
fritz
fritz 2019-12-3
回答: Raag 2025-3-7
Hi,
so I've written a function in matlab which gives six inputs to a number of neuralnetworks saved in "nets", which is a cell array and returns a vector, containing one result per network:
function e = network_evaluation(s0,s1,s2,s3,s4,s5)
var = load("Network_single_output(6-2-1).mat", "nets");
in = [s0, s1, s2, s3, s4, s5]';
n = var.nets;
e = zeros(1,length(n));
for i = 1:length(n)
e(i) = n{i}(in);
end
end
I have then used to Matlab Compiler SDK to get a Python Package which I succesfully installed and called in Python.
When I run my function though, I get an error:
Warning: Class 'network' is an unknown object class or does not have a valid 'loadobj' method. Element(s) of this class in array 'nets' have been converted to structures.
> In net_ev (line 2)
Index exceeds the number of array elements (1).
Error in net_ev (line 7)
Traceback (most recent call last):
File path, line 11, in <module>
result = netting.net_ev(1,2,3,4,5,6)
File path, line 80, in __call__
nlhsWasSpecified, stdoutObj, stderrObj).result()
File "C:\Program Files\MATLAB\R2019b\toolbox\compiler_sdk\pysdk_py\matlab_pysdk\runtime\futureresult.py", line 135, in result
raise e
matlab_pysdk.runtime.MatlabRuntimeError: An error occurred when evaluating the result from a function. Details:
File path\net_ev.m, line 7, in net_ev
Index exceeds the number of array elements (1).
(I have replaced the acutally file path here with "path").
I have already tried to use
length(n)-1
but it didn't work. Does anyone have an idea what the issue might be?
Thanks in advance!

回答(1 个)

Raag
Raag 2025-3-7
Hi fritz,
The issue stems from how MATLAB Compiler SDK handles neural network objects. When you compile your MATLAB code into a Python package, the neural network objects are converted to plain structures. As a result, when you try to call them like functions, MATLAB can not find the expected function behaviour, leading to the "Index exceeds the number of array elements" error.
A workaround is to avoid using the network objects directly after compilation. Instead, you can "freeze" your neural network as a standalone MATLAB function using the genFunctioncommand. This converts your trained network into a regular function that can be called safely in your compiled environment.
Consider this example, suppose you have a trained network saved as net. You would generate a standalone function with:
genFunction(net, 'netFunction');
Then, modify your evaluation function to use this generated function:
function e = network_evaluation(s0, s1, s2, s3, s4, s5)
% Prepare the input vector
in = [s0; s1; s2; s3; s4; s5];
% Call the standalone network function
e = netFunction(in);
end
With this approach, when you compile your code and call it from Python, you won't encounter the object conversion issue, and the network evaluation should work without errors.
For a better understanding of the above solution, refer to the following MATLAB documentation:

类别

Help CenterFile Exchange 中查找有关 Python Package Integration 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by