Simulink Code Generation Error: Array of Classes Initalization

6 visualizzazioni (ultimi 30 giorni)
Hello,
I am trying to built a Simulink function which keeps record of the statistics of the system by using array of classes. However, I can not generate an array of class in Simulink. I realize that I should instantiate an array in Simulink but how can I instantiate a class?
arrivedArray(3, 65536) = queueInfo;
analysisArray(3, 65536) = queueInfo;
In the picture above, queueInfo is my class. This two lines return with the following error.
Code generation requires variable arrivedArray to be fully defined before subscribing it.
By the way, this is the suggested method by Matlab for creating an array of class but somehow it doesn't work. Later I've tried this one:
persistent arrivedArray
if isempty(arrivedArray)
arrivedArray(3, 655336) = queueInfo;
end
persistent analysisArray
if isempty(analysisArray)
analysisArray(3, 65536) = queueInfo;
end
But it doesn't work either. The error İs:
Persistent variable 'arrivedArray' must be assigned before it is used. The only exception is a check using 'isempty(arrivedArray)' that can be performed prior to assignment.
I understand the error but have no idea how to fix it. A constructor did not help me too. Here is my class structure:
1
classdef transactionInfo
properties
tag = uint16(0);
arrivalTime = uint64(0);
departureTime = uint64(0);
end
methods
function obj = transactionInfo(v)
if nargin > 0
obj.tag = uint16(v);
obj.arrivalTime = uint64(v);
obj.departureTime = uint64(v);
end
end
end
end
2
classdef queueInfo < transactionInfo
properties
length = uint16(0);
queueID = uint8(0);
delay = uint64(0);
end
methods
function obj = queueInfo(v)
if nargin > 0
obj.length = uint16(v);
obj.queueID = uint8(v);
obj.delay = uint64(v);
end
end
end
end
Does anyone know how to fix this issue?
Note: There might be some obvious errors in my class structures, I am kind of new to OOP. Every suggestion is welcomed.

Risposte (1)

Harsh
Harsh il 31 Gen 2025
Hi @MU,
The errors you are receiving are because of the variables not being properly instantiated before using them. Please refer to the following documentation to understand more about this - https://www.mathworks.com/help/coder/ug/resolve-issue-elements-must-be-fully-defined-before-use.html
Also, in MATLAB code generation does not support object arrays so you must create cell array for your problem. Please refer to the following documentation to understand more about this problem - https://www.mathworks.com/help/coder/ug/resolve-error-code-generation-does-not-support-object-arrays.html
Below is an example code snippet to resolve your issue –
function res = testCellArray()
%#codegen
% Define the size of the cell array
numRows = 3;
numCols = 65536;
% Initialize the cell array
arrivedArray = cell(numRows, numCols);
% Populate the cell array with instances of queueInfo
for i = 1:numRows
for j = 1:numCols
arrivedArray{i, j} = queueInfo(); % Instantiate each element
end
end
res=arrivedArray{numRows,numCols};
end

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by