0001 function ELEMSEQ = insertindrift(DRIFT0, ELEM1, POS1, varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023 if ~isatelem(DRIFT0)
0024 error('The first argument must be a valid Accelerator Toolbox drift element');
0025 end
0026
0027 if ~isatelem(ELEM1)
0028 error('The second argument must be a valid Accelerator Toolbox element to insert');
0029 end
0030
0031 if ~isnumeric(POS1)
0032 errorstr = sprintf('Incorrect syntax:\n');
0033 errorstr=[errorstr,sprintf('Elements to inserted must be followed by position [m]\n')];
0034 errorstr=[errorstr,sprintf('in the argument list: ELEM1, pos1, ... ELEMN, POSN ')];
0035 error(errorstr);
0036 end
0037
0038
0039 ELEMSEQ = {};
0040 if POS1>0
0041 ELEMSEQ{1} = atelem(DRIFT0,'Length',POS1);
0042 elseif POS1<0
0043 ('Inconsistent lengths and positions cause elements to overlap');
0044 end
0045
0046
0047 ELEMSEQ{end+1} = ELEM1;
0048 LCUM = POS1+ELEM1.Length;
0049
0050
0051 k = 1;
0052 while k < nargin-3
0053 if isatelem(varargin{k})
0054 if ~k<nargin & ~isnumeric(varargin{k+1})
0055 errorstr = sprintf('Incorrect syntax:\n');
0056 errorstr=[errorstr,sprintf('Elements to inserted must be followed by position [m]\n')];
0057 errorstr=[errorstr,sprintf('in the argument list: ELEM1, pos1, ... ELEMN, POSN ')];
0058 error(errorstr);
0059 else
0060
0061 if (varargin{k+1}-LCUM)>0
0062 ELEMSEQ{end+1} = atelem(DRIFT0,'Length', varargin{k+1} - LCUM);
0063 elseif (varargin{k+1}-LCUM)<0
0064 error('Inconsistent lengths and positions cause elements to overlap');
0065 end
0066
0067 ELEMSEQ{end+1} = varargin{k};
0068 LCUM = varargin{k+1}+varargin{k}.Length;
0069 k = k+2;
0070
0071
0072 end
0073 else
0074 error('Incorrect syntax');
0075 end
0076 end
0077
0078 if DRIFT0.Length-LCUM > 0
0079 ELEMSEQ{end+1} = atelem(DRIFT0,'Length',DRIFT0.Length - LCUM);
0080 elseif DRIFT0.Length-LCUM < 0
0081 error('Inconsistent lengths and positions cause elements to overlap');
0082 end