If x is a scalar passed to this function, then attempting to reference x(2) will error in MATLAB and in MATLAB Coder in exactly the same way for exactly the same reason. This has nothing to do with growing an array by indexing. I suspect you have tried to compile this without specifying the type of x using -args or through the coder GUI. Once you correct that issue, you need to preallocate f by
f = zeros(1,2,class(x));
Now that is because growing arrays by indexing is unsupported. Note that preallocation is preferred in MATLAB.