2

I've a set of chemical reactions, and I need to read only the first number of each chemical. For example, I've a string as

reaction = '-1.0CdCl2(aq)  1.0Cd++  2.0Cl-';

I want the find -1.0 of CdCl2(aq), 1.0 of Cd++, and 2.0 of Cl-.

chappjc
  • 30,359
  • 6
  • 75
  • 132
imransydney
  • 65
  • 1
  • 6

1 Answers1

2

textscan works here (assuming white-space delimiting the reactants):

>> C = textscan(reaction,'%f%s')
C = 
    [3x1 double]    {3x1 cell}
>> C{1}' %' decimals not shown
ans =
    -1     1     2
>> C{2}
ans = 
    'CdCl2(aq)'
    'Cd++'
    'Cl-'

Also assuming reaction starts with a number.

chappjc
  • 30,359
  • 6
  • 75
  • 132
  • Actually, I've an excel file, which I read using [ndata, text, alldata] = xlsread('react.xlsx'). The reaction data was in row 2 column 5 as reaction = '-1.0CdCl2(aq) 1.0Cd++ 2.0Cl-', which came as [1x36 char]. I used a = textscan(alldata(2,5),'%f%s'), but an error came as 'First input must be of type double or string'. I'm a beginner! So, please help. Thanks – imransydney Feb 06 '14 at 00:58
  • I guess you got this, but you want the _contents_ of that cell via `alldata{2,5}` (curly braces). – chappjc Feb 06 '14 at 01:55