How to add the recent value of a variable to the end of the name of another variable as a suffix?

I wrote a program in which there are 1000 rounds, and in some rounds under especial circumstances we need to generate a new variable. For example a zeros (2,2). But I need to generate each of these new variables like that:
If my especial circumstances satisfy the program for the first time, the program has to name my new variable: OMICRON_1
If my especial circumstances satisfy it for the second time the program has to name my new variable: OMICRON_2
And so on.
Thus in such way I can recognize how many times my especial circumstances have been satisfied and I can have all of that matrices in my work space. In other words I need to add the current value of a variable like “n” as a suffix to the end of the name of my new variable and for the next time if my especial circumstances satisfy we will have
n=n+1;

1 件のコメント

Stephen23
Stephen23 2015 年 4 月 20 日
編集済み: Stephen23 2015 年 4 月 20 日
Do not do this! Read my answer to know why...

サインインしてコメントする。

 採用された回答

Stephen23
Stephen23 2015 年 4 月 20 日
編集済み: Stephen23 2019 年 6 月 25 日

2 件のコメント

sara na
sara na 2015 年 4 月 20 日
Thanks a lot !!!
Actually I solved my problem using a "structure". In this program I've used a structure and its fields many times. But about the recent problem I was completely confused whether to use one or not! Using this link: http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
I wrote these codes:
n=1;
>> omicron.(sprintf('omicron%d', n))=n*ones(2,2);
>> n=n+1;
>> omicron.(sprintf('omicron%d', n))=n*ones(2,2);
>> n=n+1;
>> omicron.(sprintf('omicron%d', n))=n*ones(2,2);
>> omicronn=[4 5 6 7];
>> omicron.(sprintf('omicron%d', n))=omicron.(sprintf('omicron%d', n))+omicronn;
It is basically what I needed , and now I can easily work on the results.
Thank you very much!
Nice work! You might also be interested in using a non-scalar structure, which would be even simpler to use!. For each index n you simply do this:
omicron(n).data = ...
It is a very convenient way to store data, and lets you do neat things like putting all of the field-values into a cell array like this:
{omicron.data}
(read that link to know more, and also about <http://www.mathworks.com/help/matlab/matlab_prog/comma-separated-lists.html comma-separated lists).

サインインしてコメントする。

その他の回答 (1 件)

pfb
pfb 2015 年 4 月 20 日
I think this can be done through the function "eval".
However, your strategy seems impractical. Why don't you add a dimension? That is, use a "tensor"?
OMICRON(:,:,j);
If you have an idea of the max number of matrices you can possibly generate (N), you can preallocate that
OMICRON=zeros(2,2,N);
and fill as above

2 件のコメント

sara na
sara na 2015 年 4 月 20 日
編集済み: sara na 2015 年 4 月 20 日
I work with random variables and I don't have any idea about how many "omicron"s I will finally have.
As a matter of fact I prefer to have all the resulted omicron from omicron_1 to omicron_n separately in my work space.
Actually it's a good idea to use a three dimension matrix but it's impossible because I don't have any idea about how many times my circumstances will be satisfy
If you have still any idea about how I can use "eval" function or any other one to write a code don't hesitate to say them to me. Thank you
pfb
pfb 2015 年 4 月 20 日
if you do not know the dimension in advance you can keep adding new matrices at the "bottom" of the tensor.
Preallocating helps with memory management, and in general it is a good practice. But sometimes one simply has no idea, and does not preallocate.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

質問済み:

2015 年 4 月 20 日

編集済み:

2019 年 6 月 25 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by