How to store data in different variables through loop?

7 次查看(过去 30 天)
I want to store data in different variables as per my requirement, I have created different variables through the following code:
for g=1:20
eval(['Dat' num2str(g) '=g' ]);
end
Now I have variables in the form: Dat1, Dat2....Dat20. I have one large dataset named as "Data" from which I have to separate the 20 columns of Data in 20 separate variables and 28 rows. For example: Dat1 should contain 28 rows and 2nd column of Data like Dat1=Data(1:28,2) then 3rd column of Data as Dat2=Data(1:28,3) and so on till Dat20 with 21st column of Data. Please see I want to pick up values from 2nd column to 21st column and want the first variable named as Dat1. The Data originally is of 28 rows and 22 columns. I tried to tweak the code as follows:
for y=2:1:21
for g=1:20
eval(['Dat' num2str(g) '= Data(1:28,y)' ]);
end
end
But it is only storing the last column (column 21) of Data in all the variables. How can I store specified columns of Data into different created variables through loops. Anyone's help would be much appreciated. Thanks.
  3 个评论
Stephen23
Stephen23 2018-3-20
"I want to store data in different variables as per my requirement"
What exactly is that requirement? Writing code to access variable names is one way that beginners force themselves into writing slow, complex, buggy code that is hard to debug. What special "requirement" do you have that means you have to do this?
Note that the MATLAB docuemantion specifically recommends against what you are doing: "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. The preferred method is to store related data in a single array."
Note that my answer shows you how simple it is to store split the data into one cell array, following the recommendation of the MATLAB documentation.
David Fletcher
David Fletcher 2018-3-20
编辑:David Fletcher 2018-3-20
Consider what is happening: the final iteration of y will cause Dat1..Dat20 to be set to Data(1:28,21) - all exactly the same (as would happen with all other iteration values of y by the way). Mind you, if you want real advice I would suggest you tell your teachers not to set screwy homework problems that encourage bad practice like using eval to create these variables in the first place

请先登录,再进行评论。

回答(1 个)

Stephen23
Stephen23 2018-3-20
编辑:Stephen23 2018-3-20
This would be trivial with simpler, more efficient code, in just one line:
C = num2cell(Data(1:28,2:21),1);
and then access the contents of the cell array using indexing:
C{1}
C{2}
etc
Or in a loop:
for k = 1:numel(C)
C{k}
end
Doing this will be much more efficient than what you are trying to do. Note how I avoided using eval to write just one simple line of code that works, whereas your seven complex lines do not work: this is because when beginners choose to use eval they force themselves into writing slow, complex, buggy code that is hard to debug. Read this to know more:
Is there a good reason why you need to split up the data anyway? You can easily access the columns using indexing, and processing data is simpler when data is kept together, rather than split apart.

类别

Help CenterFile Exchange 中查找有关 Variables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by