Code Generation Handle Object Not Defined for All Execution Paths

3 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;
while true
% if strcmp(previousState, 'unspawned')
if ~latch
%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;
end
%% 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
if isempty(dataReceived)
pause(0.25);
else
framesReceived = framesReceived + 1;
asyncDataBuff.write(dataReceived);
%% Process data if there is enough in the buffers
if asyncDataBuff.NumUnreadSamples >= 20
X = asyncDataBuff.read(20);%waveform();
if segmentsProcessed==0
X = X + 1 ;
else
X = X + Xhold;
end
segmentsProcessed = segmentsProcessed+1;
Xhold = X;
end
end
end
end

Accepted Answer

Walter Roberson
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;
if ~latch
is too conditional for Simulink, especially with the test being inside a loop.
  8 Comments
Michael
Michael on 26 Aug 2022
Edited: Michael on 26 Aug 2022
The initial question is answered (thanks Walter) and now accepted. I'm still a bit up a creek because I don't use my object after the loop.

Sign in to comment.

More Answers (0)

Categories

Find more on Simulink Functions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!