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