using if/else statements to find molecular weight of compounds given the info from a string

7 views (last 30 days)
I'm sure there are more elegant ways of doing this, but does anyone have any suggestions to make this work? Any compound using the atoms C,H,O,S,N should be calculable. Full disclosure: I'm doing this for my intro programming class, but we are encouraged to seek help.
Here's where I am code wise:
% Calculate the mol weight of given compounds given the formula as a string
function output = molweight(str)
%calculates the molecular weight of a coumpound given its atomic structure
%in a string
if isempty(str)
error('Invalid use of molweight');
end
str = upper(str) %convert to uppercase
%Identify characters in the string
k=input('What compund do you want to check?')
i=sscanf(k, '%s',1)
if (i == C)
i=scanf(k,'*%s %f')
if isempty(i)
mass=12.0107
else
mass=12.0107*i
end
%multiply carbon value by the number following
elseif (i==H)
i=scanf(k,'*%s%f')
if isempty(i)
mass=1.0079
else
mass=1.0079*i
end
%If H is found
elseif (k==O)
i=scanf(k,'*%s%f')
if isempty(i)
mass=15.999
else
mass=15.999*i
%O
elseif (k == N)
i=scanf(k,'*%s%f')
if isempty(i)
mass=14.007
else
mass=14.007*i
%N
elseif (k == S)
i=scanf(k,'*%s%f')
if isempty(i)
mass=32.065
else
mass=32.065*i
%S
else
disp('Invalid use of molwt')
end
I know this will only calculate the first letter of the string, but was thinking of going through systematically and calculating each letter after that. Cumbersome! There must be a better way... Thanks for any help!
  2 Comments
Richard Zapor
Richard Zapor on 2 Sep 2013
The word it's is "it is" and is not possessive of it. Please remember this rule and correct your comment line of "it's atomic structure" to "its atomic structure". The word its is possessive and there is no such thing as its'.

Sign in to comment.

Accepted Answer

Richard Zapor
Richard Zapor on 2 Sep 2013
The related Cody Challenge was solved by Alfonso in one line using regexprep, strread, and str2num. To see his solution solve the Challenge using the above and then any other Cody Challenge solved will allow user to see all solutions of previously solved challenges. Your Prof is guaranteed to be flummoxed by the Alfonso solution.

More Answers (2)

Richard Zapor
Richard Zapor on 1 Sep 2013
I created a Cody Challenge Molecular Wt based on the question. Watch the Challenge as significant improvements are expected.
My initial answer to the processing is:
function mass=atomic_wt(eqn)
wtmap=zeros(83,1);
wtmap( 'CHONS'/1 )= [12 1 16 14 32];
mass=0;
ELE=regexp(eqn,'[CHONS]'); % Locate all elements
for i=1:length(ELE)
wt=wtmap(eqn(ELE(i)));
if i==length(ELE)
if ELE(end)==length(eqn) % single atom CH3OH methanol
mass=mass+wt;
else % Case of final numeric CH4
qty=str2num(eqn(ELE(i)+1:end));
mass=mass+wt*qty;
end
elseif ELE(i)+1==ELE(i+1) % single atom intra eqn
mass=mass+wt;
else % Element followed by a qty allow >9
qty=str2num(eqn(ELE(i)+1:ELE(i+1)-1));
mass=mass+wt*qty;
end
end
end

Walter Roberson
Walter Roberson on 1 Sep 2013
hint:
table1 = {'C', 'H', 'O', 'N', 'S'};
table2 = [12.0107, 1.0079, 15.999, 14.007, 32.065];
for K = 1 : length(table1);
fprintf('%2s %g\n', table1{K}, table2(K));
end

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!