Code Generation Handle Object Not Defined for All Execution Paths
7 views (last 30 days)
I have a problem where I am using a custom handle class that is allocated within an infinite while loop. At the end of each iteration through the loop, I need to store that object for use in the next time through the loop. Because these are handle obects and I am doing code generation, I can't preallocate the object before the loop. I have reduced my code to a simple example (below) where X represents this handle object. To keeps things simple, I am just making X a vector (not an instance of my class) for this example code.
As you can see below, I have tried to set up this Xhold variable on the first iteration through the loop, but I am still getting the error from coder: ??? Variable 'Xhold' is not fully defined on some execution paths.
I have seen this error in the past and have typically been able to figure out how to get around this, but in this instance I can't see how to fix this error. I don't understand why coder doesn't see that Xhold is set for the first iteration and then will be defined for other paths. If anyone has insights on why coder doesn't see Xhold as being fully defined on all execution paths I'd appreciate it.
function  = detection_debug()
coder.varsize("dataReceived",[1025 1],[1 0]);
sampsForMaxPulses = 5800320;
asyncDataBuff = dsp.AsyncBuffer(sampsForMaxPulses);
%Initialize loop variables
framesReceived = 0;
segmentsProcessed = 0;
latch = false;
% if strcmp(previousState, 'unspawned')
%Initialize; Can't do this outside (before) the loop because
%Xhold will actually be a handle objects allocated within a loop
%and thus can't be referenced outside the loop.
Xhold = rand(2,22);%Define twice so coder knows it might vary.
Xhold = rand(1,20);
latch = true;
%% Randomly generate random data. (Some times no data is generated)
[dataReceived] = rand(randi([0 1],1,1),1);%channelreceiver('0.0.0.0', Config.portData,resetUdp,false);
%% Wait for new data else put data in buffers
framesReceived = framesReceived + 1;
%% Process data if there is enough in the buffers
if asyncDataBuff.NumUnreadSamples >= 20
X = asyncDataBuff.read(20);%waveform();
X = X + 1 ;
X = X + Xhold;
segmentsProcessed = segmentsProcessed+1;
Xhold = X;
Walter Roberson on 25 Aug 2022
Edited: Walter Roberson on 25 Aug 2022
Variables must be initiallized unconditionally in the flow they are used in, with one exception:
If the variable is declared persistent or global, then it may appear conditionally in an "if isempty()" clause and defined there. Simulink recognizes that construct as a initialization.
latch = false;
is too conditional for Simulink, especially with the test being inside a loop.