Code Generation Handle Object Not Defined for All Execution Paths

3 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Walter Roberson
Walter Roberson el 25 de Ag. de 2022
Editada: Walter Roberson el 25 de Ag. de 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 comentarios
Image Analyst
Image Analyst el 25 de Ag. de 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
Michael el 26 de Ag. de 2022
Editada: Michael el 26 de Ag. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre MATLAB Coder en Help Center y File Exchange.

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by