How to add the recent value of a variable to the end of the name of another variable as a suffix?
6 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commento
Risposta accettata
Stephen23
il 20 Apr 2015
Modificato: Stephen23
il 25 Giu 2019
2 Commenti
Stephen23
il 20 Apr 2015
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).
Più risposte (1)
pfb
il 20 Apr 2015
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 Commenti
pfb
il 20 Apr 2015
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.
Vedere anche
Categorie
Scopri di più su Variables in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!