Path: news.mathworks.com!not-for-mail
From: "Ashish Uthama" <first.last@mathworks.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Atomic Weight
Date: Thu, 08 Oct 2009 09:05:16 -0400
Organization: TMW
Lines: 70
Message-ID: <op.u1hfy2tda5ziv5@uthamaa.dhcp.mathworks.com>
References: <hagkeo$pda$1@fred.mathworks.com>
NNTP-Posting-Host: uthamaa.dhcp.mathworks.com
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15; format=flowed; delsp=yes
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1255007117 5686 172.31.57.126 (8 Oct 2009 13:05:17 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 8 Oct 2009 13:05:17 +0000 (UTC)
User-Agent: Opera Mail/10.00 (Win32)
Xref: news.mathworks.com comp.soft-sys.matlab:575902


On Tue, 06 Oct 2009 19:39:04 -0400, steven berruyer  
<steven.berruyer@voila.fr> wrote:

> Hello,
>
> I try to right a program to calcul the atomic weight of molecules.
> I have the periodic table in cells.
>
> My prog is:
>
> load tableperiodique
>
> FormChim=input('Entrez dans un ensemble de cellule le symbole chimique  
> des atomes composant la mol?cule s?par?s par des espaces: \n','s');
> NbOcc=input('Entrez une matrice de m?me dimensions que la pr?c?dente  
> contenant le nombre d''occurrence de chacun des atomes dans la formule  
> chimique: \n')
>
>
> MasseAt=0;
>  for el=1:length(NbOcc)
>     for i=1:109
>          if isequal(table_periodique{i,2},FormChim{el})
>              MasseAt=MasseAt+table_periodique{i,1}*NbOcc(el);
>           end
>     end
>  end
>
> MasseAt
>
> It's function but it's not "user friendly", because for the name of  
> molecule, user has to write: {'O' 'H'} for example
> and for the number of atom : [1 2]
> I want to try to ameliorate that. I want the user have to write: H 2 O 1
> but I don't know how to indicate to Matlab that H and O are letters. And  
> how to delete the blank beetween the characters....
>
> Is there a special function? Maybe I have to use cells...I don't know.
> Does anyone can help me?
>
> Thanks a lot
>
> Steven

This is the simplest I could cook up:


str='C 1 H 3 C 1 O 1 O 1 Na 1'; %assuming strict 'symbol number' order for  
input data

remainder='somethingNonEmptyToGetTheWhileLoopStarted';

while(~isempty(remainder))

     [symbol, remainder] = strtok (str); %help strtok
     str=remainder;

     [number, remainder] = strtok (str);
     number=str2double(number); %you can use number to perform  
multiplications
     str=remainder;

     %To ensure you have only alphabets in symbol and numeric
     %(digit) in number, you could use is ISSTRPROP

     %We need to convert number back to a string to display it
     disp(['Element ' symbol ' is present ' num2str(number) ' time(s)']);

end