numAtomsOfElementInFormula

PURPOSE ^

returns the number of atoms of a single element in a formula

SYNOPSIS ^

function N=numAtomsOfElementInFormula(formula,element)

DESCRIPTION ^

 returns the number of atoms of a single element in a formula

 INPUT
 formula       formula in format Element then NumberOfAtoms with no spaces
 element       Abbreviation of element e.g. C or Mg

 OUTPUT
 N             number of atoms of this element in the formula provided

 Ronan Fleming 9 March 09
 Ronan Fleming 21 July 09 handles formulae with same first letter for
                          element e.g.C & Co in 'C55H80CoN15O11'
 Ronan Fleming 18 Sept 09 handles composite formulae like C62H90N13O14P.C10H11N5O3.Co
 Hulda SH 7 July 2011 Simplified and generalized code. Now handles most or
                      all exceptional formulas.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function N=numAtomsOfElementInFormula(formula,element)
0002 % returns the number of atoms of a single element in a formula
0003 %
0004 % INPUT
0005 % formula       formula in format Element then NumberOfAtoms with no spaces
0006 % element       Abbreviation of element e.g. C or Mg
0007 %
0008 % OUTPUT
0009 % N             number of atoms of this element in the formula provided
0010 %
0011 % Ronan Fleming 9 March 09
0012 % Ronan Fleming 21 July 09 handles formulae with same first letter for
0013 %                          element e.g.C & Co in 'C55H80CoN15O11'
0014 % Ronan Fleming 18 Sept 09 handles composite formulae like C62H90N13O14P.C10H11N5O3.Co
0015 % Hulda SH 7 July 2011 Simplified and generalized code. Now handles most or
0016 %                      all exceptional formulas.
0017 
0018 if any(~isletter(element))
0019     disp(element)
0020     error('Element must not contain numbers')
0021 end
0022 
0023 if length(element)==1
0024     if ~strcmp(upper(element),element)
0025         disp(element)
0026         error('Single letter element must not be lower case')
0027     end
0028 end
0029 
0030 if ~isletter(formula(1))
0031     disp(formula)
0032     error('Formula format expected is element then number of elements, not the other way around')
0033 end
0034 
0035 zero='0';
0036 indZero=strfind(formula,zero);
0037 if ~isempty(indZero)
0038     if isletter(formula(indZero-1))
0039         % formula = strrep(formula, zero, 'O');
0040         error('Formula contains a zero with a letter preceeding it, represent oxygen with a the character O not zero')
0041     end
0042 end
0043 
0044 %Count FULLR and FULLR2 groups as R groups (Human reconstruction)
0045 formula = strrep(formula, 'FULLR2', 'R');
0046 formula = strrep(formula, 'FULLR', 'R');
0047 
0048 if ischar(element) && ischar(formula)
0049     elementStart = regexp(formula, '[A-Z]', 'start'); % Get indices of all capital letters in the string formula. Treated as starting indices of elements.
0050     elementArray = cell(length(elementStart),2); % Initialize cell array for element symbols (Column 1) and numerical subscripts (Column 2)
0051     
0052     for n = 1:length(elementStart) % Loop for each element in formula
0053         if n < length(elementStart)
0054             splitFormula = formula(elementStart(n):(elementStart(n+1)-1)); % Extract section of formula from starting index of element n to starting index of element n+1
0055             
0056         else
0057             splitFormula = formula(elementStart(n):end);
0058             
0059         end
0060         
0061         if ~isempty(regexp(splitFormula, '[^a-z_A-Z]', 'once')) % If current section of formula contains non-alphabetic characters
0062             elementArray{n,1} = splitFormula(1:(regexp(splitFormula, '[^a-z_A-Z]', 'once')-1)); % Element symbol assumed to extend from beginning of current section to the first non-alphabetic character
0063             rest = splitFormula(regexp(splitFormula, '[^a-z_A-Z]', 'once'):end); % Rest of section after element symbol.
0064             
0065             if ~isempty(regexp(rest, '\W', 'once')) % Rest of section may contain word characters such as numbers and dots.
0066                 if ~isempty(regexp(rest(1), '\d', 'once')) % If first character following element symbol is numeric it is assumed to represent the number of atoms of that element.
0067                     elementArray{n,2} = rest(1:(regexp(rest, '\W', 'once')-1)); % Extract element's numeric subscript.
0068                     
0069                 else
0070                     elementArray{n,2} = '1'; % If no number follows element symbol the numeric subscript is assumed to be 1.
0071                     
0072                 end
0073                 
0074             else
0075                 elementArray{n,2} = rest;
0076             
0077             end
0078             
0079         else
0080             elementArray{n,1} = splitFormula;
0081             elementArray{n,2} = '1';
0082             
0083         end
0084     end
0085     
0086     elementRows = strmatch(element, elementArray(:,1), 'exact'); % Element may appear in two different locations within formula.
0087     
0088     if ~isempty(elementRows)
0089         elementCount = zeros(length(elementRows),1);
0090         
0091         for m = 1:length(elementRows)
0092             elementCount(m,1) = str2double(elementArray{elementRows(m),2}); % Get numeric subscript by each instance of element in formula and convert from char to double
0093             
0094         end
0095         
0096         N = sum(elementCount);
0097         
0098     else
0099         N = 0;
0100         
0101     end
0102     
0103 else
0104     if ~ischar(element)
0105         disp(element)
0106         error('Element must be given by a variable of class char')
0107     else
0108         disp(element)
0109         error('Formula must be given by a variable of class char')
0110     end
0111 end

Generated on Thu 21-Jun-2012 15:39:23 by m2html © 2003