Creating a new variable in each iteration...

I want to create a new variable in each iteration of a for loop... x_1, x_2, x_3, etc... up to x_50
count = 0;
for i = 2:2:100
count = count + 1;
x_(...I want "count" to go in here... what is the code to achieve this?...) = i^2
end
Basically, there should be an x_1 = 4 created in the first iteration, x_2 = 16 in the second iteration, etc... up to x_50 = 10000
Thanks
D Howard
Note: I have seen others ask similar questions, with experienced users saying things like "this is a bad idea because it is hard to reference these later..." and they suggest creating a vector, but my problem is more general and this is not an option.

 Réponse acceptée

James Tursa
James Tursa le 20 Fév 2012
E.g., with the usual caveats about this typically being a bad idea:
eval(['x_' num2str(count) ' = i^2']);

11 commentaires

James Tursa
James Tursa le 20 Fév 2012
P.S. I would be curious to know why using a vector or cell array is not a viable option for you.
Doron
Doron le 20 Fév 2012
Thank you very much James, I am about to try use this.
The reason I cannot use a vector is because the variables I will be creating in my loop are matrices...
Jiro Doke
Jiro Doke le 20 Fév 2012
If each loop creates a matrix, then you can still use "cell arrays" or "structures"
Doron
Doron le 20 Fév 2012
Why is this better than variable creation?
Jiro Doke
Jiro Doke le 20 Fév 2012
The FAQ link that Walter referenced is a good one. There are a number of reasons, but I like the readability and performance.
for i = 1:100
A{i} = i^2
end
is so much easier to read and understand than
for i = 1:100
eval(['x_' num2str(i) ' = i^2;']);
end
It also depends on what you want to do with the data afterwards. For example, I may need to access the data for analysis or plotting:
for i = 1:100
doSomeAnalysisAndPlot(A{i});
end
Which is again easier to understand than
for i = 1:100
doSomeAnalysisAndPlot(eval(['x_' num2str(i)]));
end
Finally, there's a bunch of optimization going on with MATLAB to make things run faster. Calling your data as "A{i}" is faster than "eval(['x_' num2str(i)])". It may not be an issue with your particular data size, but if you start working with large data in the future, you'll regret it if you keep using "eval" like that. So my argument is why not learn it the recommended way if it doesn't hurt you now but may help you *a lot* in the future.
Doron
Doron le 20 Fév 2012
Thanks Jiro,
You convinced me...
I decided to use the array now.
If I have a matrix called P, do I need to be careful about naming an array P{i} too?
D Howard
Doron
Doron le 20 Fév 2012
Thanks Jiro,
You convinced me...
I decided to use the array now.
If I have a matrix called P, do I need to be careful about naming an array P{i} too?
D Howard
Jiro Doke
Jiro Doke le 20 Fév 2012
Great!
I'm not sure I understand the question. The nice thing about using a cell array is that you'll only have one variable. You name it whatever you like, e.g. "myData". Each individual components are simply stored/accessed by using the index number, i.e. you don't name it.
Then if you want to see the contents of the 8th component, you just refer to it as "myData{8}"
Does that make sense?
Doron
Doron le 20 Fév 2012
Yes Jiro, this makes sense to me...
But, say I have a matrix called P, and I want to split it into 10 smaller matrices... Can I name the array P? and in doing so call each little matrix P{1}, P{2}... P{10}?
...or, will this wipe out my original matrix P?
D Howard
Jiro Doke
Jiro Doke le 20 Fév 2012
Oh I see. Right, you don't want to do that. If you have P already and want to break it up, you should create a different cell array with a different name. Actually, it probably won't let you create it and give you an error.
You may also want to take a look at the function "mat2cell", that allows you to take a regular matrix P and convert to a cell array.
Doron
Doron le 20 Fév 2012
Ok Jiro, I am about to ask post question directly related to this...
...This is going somewhere, believe me.
Doron

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 20 Fév 2012

2 votes

As best I recall at the moment, I have not had a need for dynamic variable names since the days I programmed in REXX ... 25 years ago.

1 commentaire

Doron
Doron le 20 Fév 2012
Hello Walter,
Thank you for this link. I saw that you commented on my original posting (my other question) on plotting from a matrix... there was some confusion about what I want to do.
I rewrote the question to a specific example, you assistance would be greatly appreciated.
D Howard

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by