MATLAB: Simulink function variable output problem

MATLABvariable size

I have a function that lets say can have an output of variable size array between 1 and 10 elements I have set y as variable size as [1,10] in the Edit Data window The code in if 0 … works correctly and return variable size arrays of size 9 or 10 When I try to run code above if 0 i get (initially was commented)
Size mismatch (size [0 x 0] ~= size [1 x 2]).
The size to the left is the size of the left-hand side of the assignment.
Function 'FrameGenerator/AddEscapeCharacter1/MATLAB Function' (#23.281.291), line 9, column 3:
"finalArray"
How to fix this lol ?
Here is the code
function y = addEscapeCharacter(frameBytes, escapeCharacter, startOfFrame) %codegen
finalArray = single([]);
%coder.varsize('y',[1,10]);
for i =1:length(frameBytes)
currentFrameByte = frameBytes(i);
if currentFrameByte == escapeCharacter || currentFrameByte == startOfFrame
finalArray = horzcat([escapeCharacter,frameBytes(i)], finalArray);
else
finalArray = horzcat([currentFrameByte], finalArray);
end
end
if 0
if randi(2) ==1
finalArray = horzcat(single( [1 2 3 4 5 6 7 8 9]) , single([6]));
else
finalArray = horzcat(single( [1 2 3 4 5 6 7]), single([6]), single([1]));
end
end
%print('%d', length(finalArray));
y = single(finalArray);

Best Answer

Ok i solved it with this modifications
function y = addEscapeCharacter(frameBytes, escapeCharacter, startOfFrame) %codegen
finalArray = single([]);
coder.varsize('finalArray',[1, 2 * length(frameBytes)]);
for i =1:length(frameBytes)
currentFrameByte = frameBytes(i);
if currentFrameByte == escapeCharacter || currentFrameByte == startOfFrame
finalArray = horzcat(finalArray,[escapeCharacter, currentFrameByte]);
else
finalArray = horzcat(finalArray,[currentFrameByte]);
end
end
y = single(finalArray);
I hope is the most efficient way This function adds to a frame escape characters for ESC char and start of frame as described in the Link Layer OSI Framing protocol