You should read the comments at the top of the function and understand them.
In particular, the function must be initialised first by calling it with
trajectory_generator(, , waypoints);
where waypoints is a 3xN matrix.
Only then can you use it with
desired_state = traj_generator(t, state);
The error you see occurs when traj_time is empty (end is indeed an invalid index in that case). And traj_time can only be empty if the function has not been initialised.
Granted, the function should check that it's been initialised before using the persistent variables. You should add than check.
...and take out the nested functions which should be local functions
...and indent the code properly to make it easier to read and debug
...and fix all the missing semicolons and any other mlint warning