...the script ... only write[s] to the index (:,:,1)
Yes, because your index-computing expression only addresses a plane in the 3D array, not the whole array. In the expression size(H(:,:,length(frequencies))), the answer is the x,y dimension of a plane so you compute the linear address in the plane, not in the array.
instead for each plane k and then
You don't need (or want) the subsequent H(:,:,k) assignment at all--that's what the linear addresses will take care of when you convert them to 3D indexing.