0001 function elstr=at2str(elem)
0002
0003
0004
0005
0006
0007 atclass=atguessclass(elem, 'UseClass');
0008
0009 switch atclass
0010 case 'Drift'
0011 create=@atdrift;
0012 [options,args]=doptions(elem,create,{'Length'});
0013 case 'Quadrupole'
0014 create=@atquadrupole;
0015 args={elem.Length,elem.PolynomB(2)};
0016 [options,args]=doptions(elem,create,{'Length','K'},{'PolynomB','PolynomA'},args);
0017 if ~any(abs(getfield2(elem,'PolynomA',0))>0)
0018 options=rmfield2(options,{'PolynomA'});
0019 end
0020 anom=abs(getfield2(elem,'PolynomB',0))~=0;
0021 anom(2)=false;
0022 if ~any(anom)
0023 options=rmfield2(options,{'PolynomB'});
0024 end
0025 case 'Sextupole'
0026 create=@atsextupole;
0027 args={elem.Length,elem.PolynomB(3)};
0028 [options,args]=doptions(elem,create,{'Length'},{'PolynomB','PolynomA'},args);
0029 if ~any(abs(getfield2(elem,'PolynomA',0))>0)
0030 options=rmfield2(options,{'PolynomA'});
0031 end
0032 anom=abs(getfield2(elem,'PolynomB',0))~=0;
0033 anom(3)=false;
0034 if ~any(anom)
0035 options=rmfield2(options,{'PolynomB'});
0036 end
0037 case 'Bend'
0038 args={elem.Length,elem.BendingAngle,elem.PolynomB(2)};
0039 [options,args]=doptions(elem,@atsbend,{'Length','BendingAngle','K'},...
0040 {'EntranceAngle','ExitAngle','PolynomB','PolynomA'},args);
0041 angle1=getfield2(elem,'EntranceAngle',0);
0042 angle2=getfield2(elem,'ExitAngle',0);
0043 if all([angle1 angle2] == 0.5*elem.BendingAngle)
0044 create=@atrbend;
0045 options=rmfield2(options,{'EntranceAngle','ExitAngle'});
0046 else
0047 create=@atsbend;
0048 if angle1 == 0
0049 options=rmfield2(options,{'EntranceAngle'});
0050 end
0051 if angle2 == 0
0052 options=rmfield2(options,{'ExitAngle'});
0053 end
0054 end
0055 if ~any(abs(getfield2(elem,'PolynomA',0))>0)
0056 options=rmfield2(options,{'PolynomA'});
0057 end
0058 anom=abs(getfield2(elem,'PolynomB',0))~=0;
0059 anom(2)=false;
0060 if ~any(anom)
0061 options=rmfield2(options,{'PolynomB'});
0062 end
0063 case 'Corrector'
0064 create=@atcorrector;
0065 [options,args]=doptions(elem,create,{'Length','KickAngle'});
0066 case 'Multipole'
0067 create=@atmultipole;
0068 [options,args]=doptions(elem,create,{'Length','PolynomA','PolynomB'});
0069 case 'ThinMultipole'
0070 create=@atthinmultipole;
0071 [options,args]=doptions(elem,create,{'PolynomA','PolynomB'});
0072 case 'RFCavity'
0073 create=@atrfcavity;
0074 [options,args]=doptions(elem,create,{'Length','Voltage','Frequency','HarmNumber','Energy'});
0075 case 'RingParam'
0076 create=@atringparam;
0077 [options,args]=doptions(elem,create,{'Energy','Periodicity'});
0078 case 'Aperture'
0079 create=@ataperture;
0080 [options,args]=doptions(elem,create,{'Limits'});
0081 case 'QuantDiff'
0082 create=@atQuantDiff;
0083 [options,args]=doptions(elem,create,{'Lmatp'});
0084 case 'Matrix66'
0085 create=@atM66;
0086 [options,args]=doptions(elem,create,{'M66'});
0087 case 'MatrixTijkPass'
0088 create=@atM66Tijk;
0089 [options,args]=doptions(elem,create,{'M66','Tijk'});
0090 case 'Monitor'
0091 create=@atmonitor;
0092 [options,args]=doptions(elem,create);
0093 otherwise
0094 create=@atmarker;
0095 [options,args]=doptions(elem,create);
0096 if isfield(options,'Energy')
0097 options=rmfield(options,'Energy');
0098 end
0099 end
0100 varg=[args,reshape(cat(2,fieldnames(options),struct2cell(options))',1,[])];
0101 fmt=['%15s(%s' repmat(',%s',1,length(varg)-1) ')'];
0102
0103 strargs=cellfun(@(arg) mat2str(reshape(arg,[],size(arg,2))),varg,'UniformOutput',false);
0104 elstr=sprintf(fmt,func2str(create),strargs{:});
0105
0106 function s2 = rmfield2(s1,fieldnames)
0107 ok=cellfun(@(fn) isfield(s1,fn), fieldnames);
0108 s2=rmfield(s1,fieldnames(ok));
0109 end
0110
0111 function result = getfield2(s,varargin)
0112 try
0113 result=getfield(s,varargin{1:end-1});
0114 catch
0115 result=varargin{end};
0116 end
0117 end
0118
0119 function [opts,args]=doptions(elem,create,argn,dontcheck,args)
0120 if nargin<4, dontcheck={}; end
0121 if nargin<3, argn={}; end
0122 if nargin<5, args=cellfun(@(field) elem.(field),argn,'UniformOutput',false); end
0123 args=[{elem.FamName},args];
0124 defelem=create(args{:});
0125 if ~strcmp(elem.PassMethod,defelem.PassMethod)
0126 args=[args,{elem.PassMethod}];
0127 end
0128 argnames=[{'FamName'},argn,{'PassMethod'}];
0129 defel=rmfield(defelem,[argnames,dontcheck]);
0130 fnames=fieldnames(defel)';
0131 fvalues=struct2cell(defel)';
0132 ok=isfield(elem,fnames);
0133 ok(ok)=cellfun(@(fname,fval) isequal(elem.(fname),fval),fnames(ok),fvalues(ok));
0134 opts=rmfield2(elem,[argnames,fnames(ok)]);
0135 end
0136 end