Azzera filtri
Azzera filtri

change variable name in each loop

61 visualizzazioni (ultimi 30 giorni)
Oliver Kumar
Oliver Kumar il 19 Mag 2016
Modificato: Stephen23 il 25 Giu 2019
Hello,
I'm trying do make a for loop in whicht the variable name changes for every loop. VPN1, VPN2, VPN3... (['VPN' num2str(l)]) When I do this, I get ans VPN1. But the problem is, that I dont get the variable VPN1 to which I can save more information. Can you please help me to solve that. Thanks.
Kind regards, Oliver
  2 Commenti
Stephen23
Stephen23 il 19 Mag 2016
Modificato: Stephen23 il 25 Giu 2019
@Oliver Kumar: Just use one variable and indexing: this is much more robust than trying to write buggy, slow code that creates or accesses lots of separate variables.
Oliver Kumar
Oliver Kumar il 19 Mag 2016
Thanks Stephen Ok, I see it's a very bad idea ;) I did it now with a structure and it works fine. Thanks!

Accedi per commentare.

Risposta accettata

Stephen23
Stephen23 il 19 Mag 2016
Modificato: Stephen23 il 25 Giu 2019
  4 Commenti
Patrick
Patrick il 13 Feb 2018
It would be nice to be able to answer the original question. I would like to do this to get a vast number of price series into a structure. For example, I have separate price vectors Price1, Price2, ... I want a loop that adds all of these vectors to a structure.
Stephen23
Stephen23 il 13 Feb 2018
Modificato: Stephen23 il 13 Feb 2018
@Patrick: the page that I linked to at the very end of my last comment mentions the function that you would need to use seventy-three times, and has links to numerous examples and threads from this forum. Take a look.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements 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!

Translated by