Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. % Partial solution to Lab 9, the 'elegant' way (using regular expressions)
  2. %
  3. % Note : Extreme hackiness required because MATLAB is shithouse at string
  4. % handling and requires strings to be stored in cell arrays.
  5. %
  6. % Note: Uses the containers.Map data structure, only available in MATLAB
  7. % r2008b and better. (Why didn't MATLAB have an associative array data type
  8. % in the first place?!)
  9. %
  10. % Li-aung Yip, liaung.yip@ieee.org
  11.  
  12. ElementSymbols = { 'H', 'He', 'Li', 'Be', 'O' };
  13.  
  14. % Mapping of element symbols to element weights.
  15. ElementWeights = containers.Map({ 'H', 'He', 'Li', 'Be', 'O', 'Fe' },{ 1, 2, 3, 4, 16,26 });
  16.  
  17. Input = 'Fe2H2SO4Fe2O3';
  18.  
  19. % Step 1 : "Tokenise" the input string.
  20. % That is, split the input into (element symbol)(number of atoms) pairs like so:
  21. % Water: 'H20' -> 'H2', 'O'
  22. % Sulphuric Acid 'H2SO4' -> 'H2', 'S', 'O4'
  23. % Rust 'Fe2O3' -> 'Fe2', 'O3'
  24. % Hexane 'CH3CH2CH2CH2CH2CH3' -> 'C', 'H3', 'C', 'H2', .... 'C','H3'.
  25. %
  26. % What's the pattern here? Each 'token' consists of an element symbol,
  27. % optionally followed by a number. (An element symbol consists of one
  28. % capital letter, optionally followed by some lowercase letters.)
  29.  
  30. TokenPattern = '[A-Z][a-z]*\d*';
  31. Tokens = regexp(Input,TokenPattern,'match')
  32.  
  33. for Token = Tokens
  34. % Step 2 : Convert tokens like 'Fe2' to the symbol 'Fe' and the number 2.
  35.  
  36. % Pull out the letters at the start
  37. ElementSymbol = regexp(Token,'[A-Z][a-z]*','match');
  38. ElementSymbol = ElementSymbol{1}{1}; % convert from cell array to text string.
  39.  
  40. % Pull out the number at the end
  41. NumAtoms = regexp(Token,'\d*','match');
  42. if ( isempty(NumAtoms{1}) ) %If there were no numbers, assume one atom.
  43. NumAtoms = 1;
  44. else
  45. NumAtoms = str2num(NumAtoms{1}{1});
  46. end
  47.  
  48.  
  49.  
  50.  
  51. if ( ElementWeights.isKey(ElementSymbol) );
  52. ElementWeight = ElementWeights(ElementSymbol);
  53. TokenWeight = ElementWeight * NumAtoms;
  54. fprintf('Token %-6s | Symbol: %-3s | Number of: %-3i | Weight : %i * %i = %i\n',Token{1}, ElementSymbol,NumAtoms,NumAtoms,ElementWeight,NumAtoms*ElementWeight);
  55. else
  56. fprintf('I don''t know the weight of the element symbol %s\n',ElementSymbol);
  57. end
  58.  
  59. end
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69. Sample Output:
  70.  
  71. Tokens =
  72.  
  73. 'Fe2' 'H2' 'S' 'O4' 'Fe2' 'O3'
  74.  
  75. Token Fe2 | Symbol: Fe | Number of: 2 | Weight : 2 * 49 = 98
  76. Token H2 | Symbol: H | Number of: 2 | Weight : 2 * 1 = 2
  77. I don't know the weight of the element symbol S
  78. Token O4 | Symbol: O | Number of: 4 | Weight : 4 * 16 = 64
  79. Token Fe2 | Symbol: Fe | Number of: 2 | Weight : 2 * 49 = 98
  80. Token O3 | Symbol: O | Number of: 3 | Weight : 3 * 16 = 48
  81.  
  82.  
  83. Which is most of the way to a solution.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement