Thread Subject: Variables names to change every iteration.

Subject: Variables names to change every iteration.

From: Antonio.C.M.Silva@gmail.com

Date: 16 Jan, 2008 11:06:01

Message: 1 of 5

Hi,

Searching on usenet for what I wanted was not easy, so I apologize if
this question has been already answered.

I need a way to make a output variable of a function to change
depending on the iteration cycle we are in:

Present:
[variable2M{k}]=function()

Desired:
for i=1:3
    [(variable + i + M){k}]=function()

Thus creating as an output the function result as variable1M{k};
variable2M{k}; variable3M{k}

I've tried something of the likes of:
for i=1:3
    aux=['variable' int2str(i) 'M'];
    [aux{k}]=function()

That doesn't work. It generates an error of mistypes.

Any hint would be useful,
Thanks,
Antonio

Subject: Variables names to change every iteration.

From: Anh Huy Phan

Date: 16 Jan, 2008 11:34:02

Message: 2 of 5

Antonio.C.M.Silva@gmail.com wrote in message <3722258e-24f3-
4870-8ae3-9eca8d609e31@d4g2000prg.googlegroups.com>...
> Hi,
>
> Searching on usenet for what I wanted was not easy, so I
apologize if
> this question has been already answered.
>
> I need a way to make a output variable of a function to
change
> depending on the iteration cycle we are in:
>
> Present:
> [variable2M{k}]=function()
>
> Desired:
> for i=1:3
> [(variable + i + M){k}]=function()
>
> Thus creating as an output the function result as
variable1M{k};
> variable2M{k}; variable3M{k}
>
> I've tried something of the likes of:
> for i=1:3
> aux=['variable' int2str(i) 'M'];
> [aux{k}]=function()
>
> That doesn't work. It generates an error of mistypes.
>
> Any hint would be useful,
> Thanks,
> Antonio

Use 'eval' function.

for i=1:3
    eval(sprintf('variable%dM{k} = function;',i))
end

Anh Huy Phan
RIKEN - BSI

Subject: Variables names to change every iteration.

From: Antonio.C.M.Silva@gmail.com

Date: 16 Jan, 2008 12:42:17

Message: 3 of 5

On Jan 16, 12:34 pm, "Anh Huy Phan" <phananh...@mathworks.com> wrote:
> Antonio.C.M.Si...@gmail.com wrote in message <3722258e-24f3-
>
> 4870-8ae3-9eca8d609...@d4g2000prg.googlegroups.com>...
>
>
>
> > Hi,
>
> > Searching on usenet for what I wanted was not easy, so I
> apologize if
> > this question has been already answered.
>
> > I need a way to make a output variable of a function to
> change
> > depending on the iteration cycle we are in:
>
> > Present:
> > [variable2M{k}]=function()
>
> > Desired:
> > for i=1:3
> > [(variable + i + M){k}]=function()
>
> > Thus creating as an output the function result as
> variable1M{k};
> > variable2M{k}; variable3M{k}
>
> > I've tried something of the likes of:
> > for i=1:3
> > aux=['variable' int2str(i) 'M'];
> > [aux{k}]=function()
>
> > That doesn't work. It generates an error of mistypes.
>
> > Any hint would be useful,
> > Thanks,
> > Antonio
>
> Use 'eval' function.
>
> for i=1:3
> eval(sprintf('variable%dM{k} = function;',i))
> end
>
> Anh Huy Phan
> RIKEN - BSI

Thanks, served perfectly.

Antonio

Subject: Variables names to change every iteration.

From: Loren Shure

Date: 16 Jan, 2008 12:42:42

Message: 4 of 5

In article <3722258e-24f3-4870-8ae3-9eca8d609e31
@d4g2000prg.googlegroups.com>, Antonio.C.M.Silva@gmail.com says...
> Hi,
>
> Searching on usenet for what I wanted was not easy, so I apologize if
> this question has been already answered.
>
> I need a way to make a output variable of a function to change
> depending on the iteration cycle we are in:
>
> Present:
> [variable2M{k}]=function()
>
> Desired:
> for i=1:3
> [(variable + i + M){k}]=function()
>
> Thus creating as an output the function result as variable1M{k};
> variable2M{k}; variable3M{k}
>
> I've tried something of the likes of:
> for i=1:3
> aux=['variable' int2str(i) 'M'];
> [aux{k}]=function()
>
> That doesn't work. It generates an error of mistypes.
>
> Any hint would be useful,
> Thanks,
> Antonio
>

You can do it with eval, but it's better not to. For rationale AND
other ways to accomplish a similar goal, please read this section of the
FAQ:
http://matlabwiki.mathworks.com/MATLAB_FAQ#How_can_I_create_variables_A1
.2C_A2.2C....2CA10_in_a_loop.3F



--
Loren
http://blogs.mathworks.com/loren/

Subject: Variables names to change every iteration.

From: Peter Boettcher

Date: 16 Jan, 2008 14:35:57

Message: 5 of 5

Antonio.C.M.Silva@gmail.com writes:

> Hi,
>
> Searching on usenet for what I wanted was not easy, so I apologize if
> this question has been already answered.
>
> I need a way to make a output variable of a function to change
> depending on the iteration cycle we are in:
>
> Present:
> [variable2M{k}]=function()
>
> Desired:
> for i=1:3
> [(variable + i + M){k}]=function()
>
> Thus creating as an output the function result as variable1M{k};
> variable2M{k}; variable3M{k}
>
> I've tried something of the likes of:
> for i=1:3
> aux=['variable' int2str(i) 'M'];
> [aux{k}]=function()

You already know how to use a cell array. So why not use another cell
array?

variable{i}{k} = function()

-Peter

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com