change variable name in each loop

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 个评论

@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.
Thanks Stephen Ok, I see it's a very bad idea ;) I did it now with a structure and it works fine. Thanks!

请先登录,再进行评论。

 采纳的回答

Stephen23
Stephen23 2016-5-19
编辑:Stephen23 2019-6-25

4 个评论

What if I have 59 GB of information that I need to access? I'm not going to create a 59 GB struct. Sometimes the data needs to be broken down into smaller chunks. In order to do that, the deconstruction process needs to be batched and the resultant variable names need some sort of sequencing so they can be accessed efficiently later. Please stop posting the same answer every time someone has this problem, it assumes everybody's work is the same. How can I create a bunch of smaller variables/structures with some sort of index in the variable name?
@Richard Simmons: Does your MATLAB have more than 100 GB of memory available? You can check this using the memory command. Presuming that you have such a large system, lets investigate some of your points:
"I'm not going to create a 59 GB struct"
Why not? A struct is not stored as one contiguous array in memory: the struct itself is one array, and each of its fields are themselves contiguous arrays. A struct is much like an array of pointers, and so the struct does not require contiguous memory for the entire data:
"Sometimes the data needs to be broken down into smaller chunks"
If you really want to, go for it. Import each data measurement into its own array (e.g. speed in one array, distance in another, etc), and then you can use indexing. Trivial.
"resultant variable names need some sort of sequencing so they can be accessed efficiently later."
Nope. To provide "some sort of sequencing" means a number provides the sequence information, but accessing the variable names to do that is not efficient code: this is what the MATLAB documentation states: "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended." Indexing is much more efficient.
"How can I create a bunch of smaller variables/structures with some sort of index in the variable name?"
Well, this would be easy for you to find out if you had actually read my answer or any of the links in it, because most of those threads discuss exactly that, and the required function is mentioned approximately a trillion times.
Of course if you had read those links you would also know why doing this is a really bad idea. I am not telling you what to do (the efficiency of your code is your problem, not mine), because all I did was to simply collect some already existing information together. What you do with that information is up to you.
PS: for such large data you should really consider using either of these:
PPS: See what I just did there? I just gave you two better options for working with large data in MATLAB. Better as in "actually will make your code more efficient", and "does not use a method that all MATLAB experts avoid using":
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.
@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.

请先登录,再进行评论。

更多回答(0 个)

类别

Community Treasure Hunt

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

Start Hunting!

Translated by