function optics = arrange_optics_along_x(optics_unsorted) % arrange optics in proper order so its x position increases with consequent index N=length(optics_unsorted); % assign x positions x=zeros(1,N); for i=1:N x(i)=optics_unsorted{i}.x; end [xs,indx]=sort(x); cntr=1; for i=indx optics{cntr}=optics_unsorted{i}; cntr=cntr+1; end end