MATLAB: The most interesting head scratching “break” command problem in Simulink these days

digital image processingdigital signal processingHDL Codersimulink

Below is a simple snippet to implement in Simulink HDL Coder, however, to my surprise the "break" command is not valid in HDL coder nor is the "for-iterator block", which has led me to wonder if its at all possible to implement this in Simulink for HDL
a) I could avoid the "for-iterator block" (because its not available in HDL), by using a user defined function block and writing the for loop in there (which is where the snippet is from below), but I'm not sure how one can get around a break command
b) I doubt one would find a command similar to "break" for HDL, and so will therefore rule out the user-defined-function block as an option, leaving you with just counter and logical blocks to solve the problem, but in which lies the dilemma
Does anyone know how you can implement the code below? I tried all sorts of limited counters and memory blocks, but cant seem to solve it and break out of the loop. Any tips and help would be greatly appreciated
function y = fcn(gs,g)
%#codegen
n = length(gs);
for i = n:-1:1
if gs(i) >= g
idx = i;
break
else
idx = 0;
end
end
y = idx;

Best Answer

In this case, since you are passing in the 80x1 matrix, here's what you do:
% returns last index that matches gs >= g
for ii = fi(1:80)
if gs(ii) >= g
idx = ii;
end
end
To use the first/smallest index of gs that is >= g, do the following:
% returns smallest index of gs that is >= g
for ii = fi(80:-1:1)
if gs(ii) >= g
idx = ii;
end
end
In both these cases, you are trying to loop over all 80 indices in one clock cycle, which means 80 comparisons in one cycle. If instead, you were able to pass in the 80x1 matrix one value at a time to the function, that would help compare one value of gs at a time.