Code Generation Handle Object Not Defined for All Execution Paths

3 vues (au cours des 30 derniers jours)
Michael le 25 Août 2022
Modifié(e) : Michael le 26 Août 2022
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;
%% Randomly generate random data. (Some times no data is generated)
[dataReceived] = rand(randi([0 1],1,1),1);%channelreceiver('', Config.portData,resetUdp,false);
%% Wait for new data else put data in buffers
if isempty(dataReceived)
framesReceived = framesReceived + 1;
%% Process data if there is enough in the buffers
if asyncDataBuff.NumUnreadSamples >= 20
X =;%waveform();
if segmentsProcessed==0
X = X + 1 ;
X = X + Xhold;
segmentsProcessed = segmentsProcessed+1;
Xhold = X;

Réponse acceptée

Walter Roberson
Walter Roberson le 25 Août 2022
Modifié(e) : Walter Roberson le 25 Août 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 commentaires
Image Analyst
Image Analyst le 25 Août 2022
Is it solved Michael? If so, please click the "Accept this answer" link to award Wlater reputation points and let others know it's solved.
Michael le 26 Août 2022
Modifié(e) : Michael le 26 Août 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.

Connectez-vous pour commenter.

Plus de réponses (0)




Community Treasure Hunt

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

Start Hunting!

Translated by