|
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
|