## Creating Variables

### Creating Variables

I was wondering if there was a way to create a new variable in each
iteration of a loop.  For example, if i want to create five variables, say
a,b,c,d,e, and assign them numbers, say 1,2,3,4,5, in a for loop.  Is there
a certain structure for this?  I know it would look something like this:

for i = 1:5
'letter' = i
end

thanks

james

### Creating Variables

James,

This is one of those questions that consistently pops up in this newsgroup
since everyone has to battle this problem sooner or later.  The answer is to
use the eval function.  For example,

cVar = {'a', 'b', 'c', 'd', 'e'};
for i = 1:5
sCmd = [cVar{i} ' = i;'];
% Alternatively:  sCmd = sprint('%s = i;', cVar{i});
eval(sCmd);
end

The idea is to build a string representation for the command you would like
to run, and then to run the command contained within the string by using the
eval command.

For another example, see the "Getting Started" documentation in the
helpdesk.  Specifically, look at the section called "The eval Function" in
the chapter called "Scripts and Functions".

Hope that helped,

Jordan

Quote:> I was wondering if there was a way to create a new variable in each
> iteration of a loop.  For example, if i want to create five variables, say
> a,b,c,d,e, and assign them numbers, say 1,2,3,4,5, in a for loop.  Is
there
> a certain structure for this?  I know it would look something like this:

> for i = 1:5
>     'letter' = i
> end

> thanks

> james

### Creating Variables

[posted and mailed]
On Sun, 27 Feb 2000 21:40:04 -0500, "James"

>I was wondering if there was a way to create a new variable in each
>iteration of a loop.  For example, if i want to create five variables, say
>a,b,c,d,e, and assign them numbers, say 1,2,3,4,5, in a for loop.  Is there
>a certain structure for this?  I know it would look something like this:

>for i = 1:5
>    'letter' = i
>end

Don't do this!

use
for i=1:5
variable{i} = whatever;
end

This will avoid cluttering your variable space, perhaps be a little
more efficient than using eval and be a lot more portable to other
languages that do not have something similar to eval. It is often a
good idea to learn programming techniques that are portable to a wide
set of systems.

Lars

------------------------------

http://www.gbar.dtu.dk/~matlg

Check out my matlab toolboxes at:
http://members.xoom.com/gregersenweb/matlab/

### Creating Variables

Lars,

Good point.  I usually do that sort of thing in my code, but I actually
wrote a file the other day that required me to create variables in the
workspace using the eval function.  There was no way around it with
cells/structures.

I guess in the future, when I answer these types of questions I'll give both
answers with preference to the cell solution.

Jordan

> [posted and mailed]
> On Sun, 27 Feb 2000 21:40:04 -0500, "James"

> >I was wondering if there was a way to create a new variable in each
> >iteration of a loop.  For example, if i want to create five variables,
say
> >a,b,c,d,e, and assign them numbers, say 1,2,3,4,5, in a for loop.  Is
there
> >a certain structure for this?  I know it would look something like this:

> >for i = 1:5
> >    'letter' = i
> >end

> Don't do this!

> use
> for i=1:5
>   variable{i} = whatever;
> end

> This will avoid cluttering your variable space, perhaps be a little
> more efficient than using eval and be a lot more portable to other
> languages that do not have something similar to eval. It is often a
> good idea to learn programming techniques that are portable to a wide
> set of systems.

>   Lars

> ------------------------------

> http://www.gbar.dtu.dk/~matlg

> Check out my matlab toolboxes at:
> http://members.xoom.com/gregersenweb/matlab/

Hi,

I would like to create a new variable in a function, but the name
depends on the contents of another variable.
Suppose I have a variable called A with contains a string 'var1' then
the new variable should have a name var1 with a certain content.

At first I thought of something like command substitution in bash, like
in: rm `which ls`
First `which ls` is substituted by bash with the output from the
command `which ls` (= /bin/ls ) ahd then it is passed to rm as an
argument, so that rm /bin/ls will be executed.
So  that would result in somehting like:
`A` = [1 2 3 ; 4 5 6];
which would be substituted with
var1 = [1 2 3 ; 4 5 6];

But I cannot find a command in Matlab that does the trick. Eventually,
it should be possible to use it with structures also, where var1 will
be the part before the dot, and `samples` (stored in B) will be the
part after the dot. So something like `A`.`B` should result in
var1.samples

How can I do this?

Maurice
--
Maurice Janssen       |   The best way to accelerate
|   a computer running Windows