From: Morten Gjerding on
Hi everyone.

Although part of this problem has been addressed before, I still haven't quite figured out the solution yet. I am working on a model to describe some effective parameters of a physical system. Since the number of species in the system changes from time to time, I would like to make my code as general as possible. Therefore I have created a cell array with the suffixes for each species:

> species = {'el' 'ed'};

My general idea was then to run iterations like this (before this i have loaded the variables: psi_el , psi_ed):

> z_deno = 0;
> for n = 1:size(species,2)
> name = char(species(n))
> z_deno = z_deno + psi_(name)/r_(name);
> end

But matlab reports the following error:

>> calceff(0.3,1e-5,1e-6,0.5,0.5,10,1000);
??? Undefined function or method 'psi_' for input arguments of type 'char'.

My own debugging tells me that the problem occurs because you cant use this method, when an expression like "psi_(name)", occurs on the right hand side of =, although it works perfectly if it is the variable which is being defined (i. e. on the left hand side of =).

I do have another solution, but not a pretty one and my question is therefore if there are anyone out there who have a good solution to this problem.

Regards,
Morten.
From: Morten Gjerding on
I found my problem. For changing variable names, you have to assign variable names like the following:

> z_deno = 0;
> for n = 1:size(species,2)
> name = char(species(n))
> z_deno = z_deno + psi.(name)/r.(name);
> end

I'm sure there is other solutions but this is sufficient for me.

Thanks for your time,
Morten.
From: Steven Lord on

"Morten Gjerding" <s082177(a)student.dtu.dk> wrote in message
news:hl0ena$gg4$1(a)fred.mathworks.com...
> Hi everyone.
>
> Although part of this problem has been addressed before, I still haven't
> quite figured out the solution yet. I am working on a model to describe
> some effective parameters of a physical system. Since the number of
> species in the system changes from time to time, I would like to make my
> code as general as possible. Therefore I have created a cell array with
> the suffixes for each species:
>
>> species = {'el' 'ed'};
>
> My general idea was then to run iterations like this (before this i have
> loaded the variables: psi_el , psi_ed):

DO NOT DO THIS. Use the approach you discovered in the other message in
this thread (dynamic field name indexing) instead. See Q4.6 in the
newsgroup FAQ for some of the reasons creating variables like this at
runtime is strongly discouraged.

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ