Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: how to rename a workspace variable in a loop?

Subject: how to rename a workspace variable in a loop?

From: Nizar

Date: 19 Jul, 2007 21:34:02

Message: 1 of 4

Hi,

I was wondering how you could rename a workspace variable in a loop? What I have is a loop that generates results which are stored in a matrix. the problem is that in the 2nd loop the matrix is overwritten. I also cannot add both matrices, because they are not of the same size.

Therefore i wanted to know how to rename the workspace variable for each loop generation. for example when using a "for" loop with i=1:1:5 how can I name the matrix M for each loop with a increment, thus M1, M2, M3, etc... so that I can analyze all the data and not that of only the last loop.

any suggestions are appreciated.

thank you

Subject: Re: how to rename a workspace variable in a loop?

From: Rick Rosson

Date: 19 Jul, 2007 17:40:24

Message: 2 of 4

Use the EVAL function

Type:

   doc eval

at the command prompt.


For example:

  for i = 1:5

     ...
     ...

     eval([ 'M' num2str(k) ' = M;' ]);

     ...
     ...

  end




"Nizar " <pakoeli@hotmail.com> wrote in message
news:f7olca$1mj$1@fred.mathworks.com...
> Hi,
>
> I was wondering how you could rename a workspace variable in a loop? What
> I have is a loop that generates results which are stored in a matrix. the
> problem is that in the 2nd loop the matrix is overwritten. I also cannot
> add both matrices, because they are not of the same size.
>
> Therefore i wanted to know how to rename the workspace variable for each
> loop generation. for example when using a "for" loop with i=1:1:5 how can
> I name the matrix M for each loop with a increment, thus M1, M2, M3,
> etc... so that I can analyze all the data and not that of only the last
> loop.
>
> any suggestions are appreciated.
>
> thank you


Subject: Re: how to rename a workspace variable in a loop?

From: Rick Rosson

Date: 19 Jul, 2007 17:41:39

Message: 3 of 4

Sorry, make that:

    eval([ 'M' num2str(i) ' = M;' ]);




"Rick Rosson" <rrosson@mathworks.com> wrote in message
news:f7oloa$75a$1@fred.mathworks.com...
> Use the EVAL function
>
> Type:
>
> doc eval
>
> at the command prompt.
>
>
> For example:
>
> for i = 1:5
>
> ...
> ...
>
> eval([ 'M' num2str(k) ' = M;' ]);
>
> ...
> ...
>
> end
>
>
>
>
> "Nizar " <pakoeli@hotmail.com> wrote in message
> news:f7olca$1mj$1@fred.mathworks.com...
>> Hi,
>>
>> I was wondering how you could rename a workspace variable in a loop? What
>> I have is a loop that generates results which are stored in a matrix. the
>> problem is that in the 2nd loop the matrix is overwritten. I also cannot
>> add both matrices, because they are not of the same size.
>>
>> Therefore i wanted to know how to rename the workspace variable for each
>> loop generation. for example when using a "for" loop with i=1:1:5 how can
>> I name the matrix M for each loop with a increment, thus M1, M2, M3,
>> etc... so that I can analyze all the data and not that of only the last
>> loop.
>>
>> any suggestions are appreciated.
>>
>> thank you
>
>


Subject: Re: how to rename a workspace variable in a loop?

From: Steven Lord

Date: 19 Jul, 2007 21:48:38

Message: 4 of 4

"Nizar " <pakoeli@hotmail.com> wrote in message <f7olca$1mj$1@fred.mathworks.com>...
> Hi,
>
> I was wondering how you could rename a workspace variable in a loop? What I have is a loop that generates results which are stored in a matrix. the problem is that in the 2nd loop the matrix is overwritten. I also cannot add both matrices, because they are not of the same size.
>
> Therefore i wanted to know how to rename the workspace variable for each loop generation. for example when using a "for" loop with i=1:1:5 how can I name the matrix M for each loop with a increment, thus M1, M2, M3, etc... so that I can analyze all the data and not that of only the last loop.

Don't. See question 4.6 in the newsgroup FAQ for some of the reasons you shouldn't do this and alternatives to doing this.

http://matlabwiki.mathworks.com/MATLAB_FAQ

--
Steve Lord
slord@mathworks.com

Tags for this Thread

Everyone's Tags:

faq

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.

Tag Activity for This Thread
Tag Applied By Date/Time
faq Steven Lord 19 Jul, 2007 17:50:22
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

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.
Related Topics