ph=[ph; track1.ph(ph_id)];
This is probably the biggest culprit -- dynamic reallocation -- if you know the final result size preallocate to that size and index into it; if you don't/can't calculate the final size a priori, then set it to a large-enough(*) size to an array of NaN and index into it. When done save with
ph=ph(isfinite(ph));
to reduce to final size.
(*) Or a fairly large size like 1000, say, and then allocate another chunk as needed.
Beyond that, think we'd need to see an example of the data sets and an example of the actual desired output to be able to see what's really going on without a lot of effort to try to dig through the (uncommented) code.